简体   繁体   English

PyQT5 嵌套布局未显示

[英]PyQT5 Nested Layouts not showing

I'm currently trying myself at PyQT5 and tried to create a custom widget which contains a nested layout with some labels.我目前正在 PyQT5 上尝试自己,并尝试创建一个自定义小部件,其中包含带有一些标签的嵌套布局。 However, when I try to run the code there are no errors thrown but the window stays blank.但是,当我尝试运行代码时,没有抛出任何错误,但窗口保持空白。 What could be the problem here?这里可能有什么问题?

class MainWindow(QMainWindow):
    def __init__(self, *args, **kwargs):
        super(MainWindow, self).__init__(*args, **kwargs)
        self.setWindowTitle("This is a test")
        devicewidget = DeviceWidget()
        self.setCentralWidget(devicewidget)

class DeviceWidget(QWidget):
    def __init__(self, *args, **kwargs):
        super(DeviceWidget, self).__init__(*args, **kwargs)
        layout = QVBoxLayout()
        save_image_btn = QPushButton("Save Image")
        restore_image_btn = QPushButton("Install Image")

        device_size_layout = QHBoxLayout()
        device_size_desc_lbl = QLabel("Space:")
        device_size_lbl = QLabel("69420MB")
        device_size_layout.addWidget(device_size_desc_lbl)
        device_size_layout.addWidget(device_size_lbl)

        layout.addWidget(save_image_btn)
        layout.addWidget(save_image_btn)
        layout.addLayout(device_size_layout)

#Initialization
app = QApplication([])
window = MainWindow()
window.show()
app.exec_()

Just to be clear, this is what I am currently trying to accomplish:需要明确的是,这就是我目前正在努力实现的目标: 在此处输入图片说明

The solution was that I forgot to set the layout in the DeviceWidget class.解决方案是我忘记在 DeviceWidget 类中设置布局。

self.setLayout(layout) or layout= QVBoxLayout(self) 

helped.有帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM