pyqt gui已从其他模块更新

时间:2019-12-18 18:03:08

标签: python pyqt pyqt5

我有一个工作代码,当我按下Start按钮时,它没有问题,它调用函数on_start_clicked并执行它。

def on_start_clicked(self):
        group_box = self.group_boxes[0]
        group_box.serial_lineedit.setText("YYYY")

但是,当我将on_start_clicked移到我的main.py上时。它不会执行,也不会给我任何错误或警告。

当我不将函数移至main.py

时,这是我的全部代码
import sys 
from PyQt5 import QtCore, QtGui, QtWidgets

class PSFuncGroupBox(QtWidgets.QGroupBox):
    def __init__(self, title, parent=None):
        super().__init__(title, parent)
        grid = QtWidgets.QGridLayout()

        self.serial_label = QtWidgets.QLabel("No:")
        self.serial_lineedit = QtWidgets.QLineEdit("XXXX")      

        grid.addWidget(self.serial_label, 0, 0)
        grid.addWidget(self.serial_lineedit, 0, 1) 

        self.setLayout(grid)

class PSCalibGroupBox(QtWidgets.QGroupBox):
    def __init__(self, title, parent=None):
        super().__init__(title, parent)
        grid = QtWidgets.QGridLayout()

        self.serial_label = QtWidgets.QLabel("No:")
        self.serial_lineedit = QtWidgets.QLineEdit("XXXX")

        grid.addWidget(self.serial_label, 0, 0)
        grid.addWidget(self.serial_lineedit, 0, 1)

        self.setLayout(grid)

class MainWindow(QtWidgets.QMainWindow):
    def __init__(self, parent=None):
        super().__init__(parent)
        self.setWindowTitle("My software")

        tab_widget = QtWidgets.QTabWidget()
        self.setCentralWidget(tab_widget)

        pstest_widget = QtWidgets.QWidget()
        tab_widget.addTab(pstest_widget, "PS Tests")

        grid_lay_1 = QtWidgets.QGridLayout(pstest_widget)
        pstest_vlay = QtWidgets.QVBoxLayout()
        grid_lay_1.addLayout(pstest_vlay, 0, 0)

        self.group_boxes = []

        for i in range(1, 9):
            title = "PS{}".format(i)
            group_box = PSFuncGroupBox(title)
            pstest_vlay.addWidget(group_box)
            self.group_boxes.append(group_box)

        self.PSFStart_btn = QtWidgets.QPushButton("Start")
        self.PSFStop_btn = QtWidgets.QPushButton("Stop")
        pstest_vlay.addWidget(self.PSFStart_btn)
        pstest_vlay.addWidget(self.PSFStop_btn)
        pstest_vlay.addStretch()

        pscalib_widget = QtWidgets.QWidget()
        tab_widget.addTab(pscalib_widget, "PS Calib")

        grid_lay_2 = QtWidgets.QGridLayout(pscalib_widget)

        pscalib_vlay = QtWidgets.QVBoxLayout()
        grid_lay_2.addLayout(pscalib_vlay, 0, 0)

        group_box = MainWindow.create_pscalib_element("PS")
        self.PSCalibStart_btn = QtWidgets.QPushButton("Calib Start")
        self.PSCalibStop_btn = QtWidgets.QPushButton("Calib Stop")

        pscalib_vlay.addWidget(group_box)
        pscalib_vlay.addWidget(self.PSCalibStart_btn)
        pscalib_vlay.addWidget(self.PSCalibStop_btn)
        pscalib_vlay.addStretch()

        self.PSFStart_btn.clicked.connect(self.on_start_clicked)

    @staticmethod
    def create_pstest_element(title):
        group_box = QtWidgets.QGroupBox(title)
        grid = QtWidgets.QGridLayout()
        group_box.setLayout(grid)

        serial_label = QtWidgets.QLabel("No:")
        serial_lineedit = QtWidgets.QLineEdit("XXXX")

        grid.addWidget(serial_label, 0, 0)
        grid.addWidget(serial_lineedit, 0, 1)

        return group_box

    @staticmethod
    def create_pscalib_element(title):
        group_box = QtWidgets.QGroupBox(title)
        flay = QtWidgets.QFormLayout()
        group_box.setLayout(flay)

        serial_lineedit = QtWidgets.QLineEdit("XXXX")
        qrcode_lineedit = QtWidgets.QLineEdit("XXXX")

        flay.addRow("Serial No:", serial_lineedit)
        flay.addRow("QR Code:", qrcode_lineedit)


        return group_box

    @QtCore.pyqtSlot()
    def on_start_clicked(self):
        group_box = self.group_boxes[0]
        group_box.serial_lineedit.setText("YYYY")


if __name__ == "__main__":
    import sys

    def on_start_clicked():
        group_box = self.group_boxes[0]
        group_box.serial_lineedit.setText("YYYY")

    app = QtWidgets.QApplication(sys.argv)
    w = MainWindow()
    w.show()
    sys.exit(app.exec_())

,而我的main.py中的以下代码将不起作用:

import dynamic_grid_gui

if __name__ == "__main__":
    import sys

    def on_start_clicked():
        group_box = window.group_boxes[0]
        group_box.serial_lineedit.setText("YYYY")

    app = QtWidgets.QApplication(sys.argv)
    window = dynamic_grid_gui.MainWindow()
    window.show()
    sys.exit(app.exec_())

    window.PSFStart_btn.clicked.connect(on_start_clicked)

1 个答案:

答案 0 :(得分:0)

app.exec_()开始一个循环,当您退出循环时,它将sys.exit

通过这种方式,您的软件将永远无法到达connect。 (您可以尝试在app.exec_()调用之前添加它)