这是我在PyQt5中的第一次试验(之前没有使用Python创建GUI的经验,但我已经使用Python编写了一些程序) . 单击按钮后,我的问题是更改窗口内容 . 我创建了我的主要课程:

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

        self.centralWidget = QStackedWidget()
        self.setCentralWidget(self.centralWidget)

        self.psuE3631A = E3631A()
        self.init_gui()

    def init_gui(self):
        self.initGUI = initGUI(self)
        self.setWindowTitle('Control panel - E3631A')
        self.centralWidget.addWidget(self.initGUI)
        self.show()

    def simple_gui(self):
        self.simpleGUI = simpleGUI(self)
        self.setWindowTitle('Control panel - E3631A')
        self.centralWidget.addWidget(self.simpleGUI)
        self.centralWidget.setCurrentWidget(self.simpleGUI)
        self.show()

    def advanced_gui(self):
        pass

    def close_window(self):
        print_comment("Exit application")
        self.close()

我试图切换beetwen initGUI和simpleGUI .

simpleGUI:

class simpleGUI(QWidget):
    def __init__(self, parent=None):
        super(simpleGUI, self).__init__(parent)

        print_comment("GUI in simple mode!")

        layout = QGridLayout()
        btn_close = QPushButton("&Exit app", self)
        btn_close.clicked.connect(self.parent().close_window)

        layout.addWidget(btn_close, 0, 1)
        layout.addWidget(btn_close, 1, 1)
        layout.addWidget(btn_close, 2, 0)
        layout.addWidget(btn_close, 2, 2)
        self.setLayout(layout)

initGUI:

class initGUI(QWidget):
    def __init__(self, parent=None):
        super(initGUI, self).__init__(parent)

        self.psuE3631A = self.parent().psuE3631A

        print_comment("GUI initialize mode!")
        layout = QGridLayout()
        self.ComboBoxPorts = QComboBox(self)
        self.ComboBoxPorts.setToolTip("Select port to connect with device - only psu E3631A will be connected!")
        self.init_comboBoxPorts()
        self.scan_ports()

        lbl_version = QLabel("ver " + VERSION)
        lbl_version.setAlignment(Qt.AlignRight)
        btn_refresh = QPushButton("&Refresh ports", self)
        btn_refresh.clicked.connect(self.refresh_port_list)
        self.btn_connect = QPushButton("&Connect", self)
        self.btn_connect.clicked.connect(self.connect_to_device)
        btn_close = QPushButton("&Exit app", self)
        btn_close.clicked.connect(self.parent().close_window)
        if USER is "ADVANCED":
            cb_preselect = QCheckBox("Preselect")
            cb_preselect.stateChanged.connect(self.check_preselect)
            layout.addWidget(cb_preselect, 3, 0)

        layout.addWidget(lbl_version, 1, 2)
        layout.addWidget(self.ComboBoxPorts, 2, 0)
        layout.addWidget(btn_refresh, 2, 1)
        layout.addWidget(self.btn_connect, 2, 2)
        layout.addWidget(btn_close, 3, 2)

        self.setLayout(layout)

我不确定上面的代码格式是否合适,所以这里是我的git repo的直接链接(还有其余的功能和文件)

这就是我得到的:

GUI layout error

我在做什么错误,在simpleGUI中,只有一个按钮,它不在正确的位置?我正确使用 QStackedWidget() 吗?我想这是我的问题,但经过大量试验,我不知道如何解决这个问题......