import os
import sys

from PyQt5 import QtGui
from PyQt5 import QtWidgets

from PyQt5.QtGui import *
from PyQt5.QtCore import *
from PyQt5.QtWidgets import *

def main():
    app = QtWidgets.QApplication(sys.argv)
    programWindow = ProgramWindow()

    programWindow.show()
    sys.exit(app.exec_())


class ProgramWindow(QtWidgets.QMainWindow):

    def __init__(self):
        QtWidgets.QMainWindow.__init__(self)

        self.setup_main_window()
        self.set_window_layout()

    def setup_main_window(self):
        self.resize( 800, 600  )
        self.setWindowTitle( "Test" )

    def set_window_layout(self):
        self.startSimulationButton = QPushButton( 'Start Simulation' )

        horizontalLayout = QHBoxLayout()
        horizontalLayout.addWidget( self.startSimulationButton )

        horizontalGroupBox = QGroupBox( "My Group" )
        horizontalGroupBox.setLayout( horizontalLayout )

        main_vertical_layout = QVBoxLayout()
        main_vertical_layout.addWidget( horizontalGroupBox )
        self.setLayout( main_vertical_layout )


if __name__ == "__main__":
    main()


当我运行它时,它显示一个空窗口,并在控制台上引发此错误:

QWidget::setLayout: Attempting to set QLayout "" on ProgramWindow "", which already has a layout

我已经看过QLayout: Attempting to add QLayout "" to QWidget "", which already has a layout了,但是我认为我没有做任何事情,这段代码有什么问题?

注释掉最后一行self.setLayout( main_vertical_layout )可以解决此问题,但是当然,屏幕上不显示任何内容。

此代码应使用布局QGroupBox创建QHBoxLayout并将QGroupBox放在布局QVBoxLayout中。并且QVBoxLayout应该附加在我的主窗口上:


QHBoxLayout
QGroupBox
QVBoxLayout
程序窗口

最佳答案

这样说@Aviad,您的代码可能如下所示:

import sys

from PyQt5.QtWidgets import (QApplication, QMainWindow, QWidget, QPushButton,
                             QHBoxLayout, QGroupBox, QVBoxLayout)

class ProgramWindow(QMainWindow):
    def __init__(self):
        QMainWindow.__init__(self)

        self.setup_main_window()
        self.set_window_layout()

    def setup_main_window(self):
        self.centralwidget = QWidget()
        self.setCentralWidget(self.centralwidget)
        self.resize( 800, 600  )
        self.setWindowTitle( "Test" )

    def set_window_layout(self):
        self.startSimulationButton = QPushButton( 'Start Simulation' )

        self.horizontalLayout = QHBoxLayout(self.centralwidget)
        self.horizontalLayout.addWidget( self.startSimulationButton )

        self.horizontalGroupBox = QGroupBox( "My Group" )
        self.horizontalLayout.addWidget( self.horizontalGroupBox )

        self.main_vertical_layout = QVBoxLayout()
        self.horizontalGroupBox.setLayout( self.main_vertical_layout )
        button1 = QPushButton( 'Button1' )
        button2 = QPushButton( 'Button2' )
        self.main_vertical_layout.addStretch(1)
        self.main_vertical_layout.addWidget( button1 )
        self.main_vertical_layout.addWidget( button2 )

def main():
    app = QApplication(sys.argv)
    programWindow = ProgramWindow()

    programWindow.show()
    sys.exit(app.exec_())

if __name__ == "__main__":
    main()


python - QWidget::setLayout:尝试在已经具有布局的ProgramWindow“”上设置QLayout“”-LMLPHP

07-27 13:17