QLayout:正在尝试将 QLayout“”添加到 QWidget“”,它已经具有布局 PyQt5 Python

QLayout: Attempting to add QLayout "" to QWidget "", which already has a layout PyQt5 Python

我正在创建一个 PyQt5 QMainWindow window,它有一个图像设置为 centralLayout。我想用 QWidget 覆盖 QMainWindow,以在 QVBoxLayout 中显示一些 QLabel。但我总是收到此控制台警告“QLayout:正在尝试将 QLayout”添加到已经具有布局的 QWidget“”。

这是我的代码

from PyQt5.QtWidgets import QMainWindow, QWidget, QVBoxLayout, QLabel
from PyQt5.QtGui import QFont, QPixmap


class GameIdle(QMainWindow):
    def __init__(self):
        super().__init__()

        self.screen_width = QApplication.desktop().width()
        self.screen_height = QApplication.desktop().height()

        self.font = QFont("Ubuntu")
        self.font.setWordSpacing(2)
        self.font.setLetterSpacing(QFont.AbsoluteSpacing, 1)

        self.master_background = QLabel(self)
        self.setCentralWidget(self.master_background)

        self.font.setWordSpacing(2)
        self.font.setLetterSpacing(QFont.AbsoluteSpacing, 1)
        self.setStyleSheet("background-color:#191F26;")

        self.move(0, 0)
        self.setFixedSize(self.screen_width, self.screen_height)
        self.showFullScreen()

        self.master_identify_device_container = QWidget()
        self.master_identify_device_container.setParent(self)
        self.identify_device_audio_player = QMediaPlayer(self)

        self.frontend()
        self.identify_device()

    def frontend(self):
  
           self.master_background.setPixmap(QPixmap("picture.jpg").scaled(self.screen_width, self.screen_height))


    def identify_device(self):

            self.master_identify_device_container.setStyleSheet("background-color:black;")
            self.master_identify_device_container.resize(self.screen_width, self.screen_height)

            self.font.setPointSize(40)

            self.device_name = QLabel(self.master_identify_device_container)
            self.device_name.setFont(self.font)
            self.device_name.setText(f"DEVICE NAME : Test Device")
            self.device_name.setStyleSheet("color:white; font-weight:bold;")

            self.ip_address = QLabel(self.master_identify_device_container)
            self.ip_address.setFont(self.font)
            self.ip_address.setText(f"IP ADDRESS : 127.0.0.1")
            self.ip_address.setStyleSheet("color:white; font-weight:bold;")

            self.device_key = QLabel(self.master_identify_device_container)
            self.device_key.setFont(self.font)
            self.device_key.setText(f"DEVICE KEY : 123456678")
            self.device_key.setStyleSheet("color:white; font-weight:bold;")

            self.api_status = QLabel(self.master_identify_device_container)
            self.api_status.setFont(self.font)
            self.api_status.setText(f"API STATUS : Up")
            self.api_status.setStyleSheet("color:white; font-weight:bold;")

            self.last_api_response_time = QLabel(self.master_identify_device_container)
            self.last_api_response_time.setFont(self.font)
            self.last_api_response_time.setText("LAST API RESPONSE : 5 seconds ago")
            self.last_api_response_time.setStyleSheet("color:white; font-weight:bold;")

            self.master_identify_device_container_layout = QVBoxLayout(self.master_identify_device_container)
            self.master_identify_device_container_layout.setSpacing(0)
            self.master_identify_device_container_layout.setContentsMargins(self.screen_width / 10,
                                                                            self.screen_height / 15,
                                                                            self.screen_width / 10,
                                                                            self.screen_height / 15)

            self.master_identify_device_container_layout.addWidget(self.device_name, alignment=Qt.AlignLeft)
            self.master_identify_device_container_layout.addWidget(self.ip_address, alignment=Qt.AlignLeft)
            self.master_identify_device_container_layout.addWidget(self.device_key, alignment=Qt.AlignLeft)
            self.master_identify_device_container_layout.addWidget(self.api_status, alignment=Qt.AlignLeft)
            self.master_identify_device_container_layout.addWidget(self.last_api_response_time, alignment=Qt.AlignLeft)

            self.master_identify_device_container.show()

提前致谢

您需要为 master_background 小部件设置布局:

self.master_identify_device_container_layout = QVBoxLayout(self.master_background)

from PyQt5.QtWidgets import QMainWindow, QWidget, QVBoxLayout, QLabel, QApplication
from PyQt5.QtGui import QFont, QPixmap
from PyQt5.Qt import *


class GameIdle(QMainWindow):
    def __init__(self):
        super().__init__()

        self.screen_width = QApplication.desktop().width()
        self.screen_height = QApplication.desktop().height()

        self.font = QFont("Ubuntu")
        self.font.setWordSpacing(2)
        self.font.setLetterSpacing(QFont.AbsoluteSpacing, 1)

        self.master_background = QLabel(self)                      
        self.setCentralWidget(self.master_background)

        self.font.setWordSpacing(2)
        self.font.setLetterSpacing(QFont.AbsoluteSpacing, 1)
        self.setStyleSheet("background-color:#191F26;")

        self.move(0, 0)
        self.setFixedSize(self.screen_width, self.screen_height)
        self.showFullScreen()

        self.master_identify_device_container = QWidget(self)       
#        self.master_identify_device_container.setParent(self)
        self.identify_device_audio_player = QMediaPlayer(self)

        self.frontend()
        self.identify_device()

    def frontend(self):
       self.master_background.setPixmap(QPixmap("opencv_color.jpg").scaled(self.screen_width, self.screen_height))

    def identify_device(self):
        self.master_identify_device_container.setStyleSheet("background-color:black;")
        self.master_identify_device_container.resize(self.screen_width, self.screen_height)

        self.font.setPointSize(40)

        self.device_name = QLabel(self.master_identify_device_container)
        self.device_name.setFont(self.font)
        self.device_name.setText(f"DEVICE NAME : Test Device")
# +++                                                          ---->   vvvvvvvvvvvvvvvvvvvvvvvv                
        self.device_name.setStyleSheet("color:white; font-weight:bold; background: transparent;")

        self.ip_address = QLabel(self.master_identify_device_container)
        self.ip_address.setFont(self.font)
        self.ip_address.setText(f"IP ADDRESS : 127.0.0.1")
        self.ip_address.setStyleSheet("color:white; font-weight:bold; background: transparent;")

        self.device_key = QLabel(self.master_identify_device_container)
        self.device_key.setFont(self.font)
        self.device_key.setText(f"DEVICE KEY : 123456678")
        self.device_key.setStyleSheet("color:white; font-weight:bold; background: transparent;")

        self.api_status = QLabel(self.master_identify_device_container)
        self.api_status.setFont(self.font)
        self.api_status.setText(f"API STATUS : Up")
        self.api_status.setStyleSheet("color:white; font-weight:bold; background: transparent;")

        self.last_api_response_time = QLabel(self.master_identify_device_container)
        self.last_api_response_time.setFont(self.font)
        self.last_api_response_time.setText("LAST API RESPONSE : 5 seconds ago")
        self.last_api_response_time.setStyleSheet("color:white; font-weight:bold; background: transparent;")

#        self.master_identify_device_container_layout = QVBoxLayout(self.master_identify_device_container)
        self.master_identify_device_container_layout = QVBoxLayout(self.master_background)

        self.master_identify_device_container_layout.setSpacing(0)
        self.master_identify_device_container_layout.setContentsMargins(self.screen_width / 10,
                                                                        self.screen_height / 15,
                                                                        self.screen_width / 10,
                                                                        self.screen_height / 15)

        self.master_identify_device_container_layout.addWidget(self.device_name, alignment=Qt.AlignLeft)
        self.master_identify_device_container_layout.addWidget(self.ip_address, alignment=Qt.AlignLeft)
        self.master_identify_device_container_layout.addWidget(self.device_key, alignment=Qt.AlignLeft)
        self.master_identify_device_container_layout.addWidget(self.api_status, alignment=Qt.AlignLeft)
        self.master_identify_device_container_layout.addWidget(self.last_api_response_time, alignment=Qt.AlignLeft)

#        self.master_identify_device_container.show()
            
if __name__ == '__main__': 
    import sys
    app = QApplication(sys.argv)
    w = GameIdle()
    w.show()
    sys.exit(app.exec_())