首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >QLayout:尝试添加QLayout“to QWidget”,它已经具有PyQt5 Python的布局

QLayout:尝试添加QLayout“to QWidget”,它已经具有PyQt5 Python的布局
EN

Stack Overflow用户
提问于 2021-04-23 13:23:46
回答 1查看 256关注 0票数 1

我正在创建一个PyQt5 QMainWindow窗口,该窗口的图像设置为centralLayout。我想将QMainWindow与QWidget覆盖起来,以便在QVBoxLayout中显示一些QLabels。但是我总是收到这个控制台警告" QLayout :试图添加QLayout‘to QWidget’,它已经有了一个布局“。

这是我的密码

代码语言:javascript
复制
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()

提前感谢

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-04-24 12:22:44

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

代码语言:javascript
复制
self.master_identify_device_container_layout = QVBoxLayout(self.master_background)

代码语言:javascript
复制
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_())            

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/67230772

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档