如何在不关闭GUI窗口的情况下停止运行PyQt5(.ui文件中的代码)程序?

时间:2020-05-17 23:16:39

标签: python pyqt5 qprocess qpushbutton

我想通过“停止”按钮停止递减计数,但是现在代码不起作用。

我试图从here实施解决方案,但未成功。 我从.ui文件生成GUI代码,而我的“ setupUi”函数有2个参数(self,MainWindow),可能会在我的代码中造成问题(在链接的示例initUI函数中只有1个argumet(self)。

'''

from PyQt5 import QtCore, QtGui, QtWidgets
import time
class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
        MainWindow.setObjectName("MainWindow")
        MainWindow.setEnabled(True)
        MainWindow.resize(1111, 321)
        MainWindow.setFocusPolicy(QtCore.Qt.NoFocus)
        MainWindow.setTabShape(QtWidgets.QTabWidget.Rounded)

        self.process = QtCore.QProcess(self)

        self.centralwidget = QtWidgets.QWidget(MainWindow)
        self.centralwidget.setObjectName("centralwidget")
        self.pushButton = QtWidgets.QPushButton(self.centralwidget)
        self.pushButton.setGeometry(QtCore.QRect(450, 90, 101, 23))
        self.pushButton.setCheckable(True)
        self.pushButton.setObjectName("pushButton")
        self.pushButton.clicked.connect(self.on_click)
        self.label_3 = QtWidgets.QLabel(self.centralwidget)
        self.label_3.setGeometry(QtCore.QRect(480, 150, 41, 31))
        font = QtGui.QFont()
        font.setPointSize(16)
        self.label_3.setFont(font)
        self.label_3.setFrameShape(QtWidgets.QFrame.NoFrame)
        self.label_3.setTextFormat(QtCore.Qt.AutoText)
        self.label_3.setScaledContents(False)
        self.label_3.setAlignment(QtCore.Qt.AlignCenter)
        self.label_3.setObjectName("label_3")
        self.pushButton_2 = QtWidgets.QPushButton(self.centralwidget)
        self.pushButton_2.setGeometry(QtCore.QRect(450, 230, 101, 23))
        self.pushButton_2.setCheckable(False)
        self.pushButton_2.setObjectName("pushButton_2")

        self.pushButton_2.clicked.connect(self.process.kill)

        MainWindow.setCentralWidget(self.centralwidget)
        self.statusbar = QtWidgets.QStatusBar(MainWindow)
        self.statusbar.setObjectName("statusbar")
        MainWindow.setStatusBar(self.statusbar)
        self.retranslateUi(MainWindow)
        QtCore.QMetaObject.connectSlotsByName(MainWindow)


    def on_click(self):
        print("I used first function")

        def Change_label():
                print("I used second function")
                self.label_3.setText('loop')
                QtGui.QGuiApplication.processEvents()
                time.sleep(0.5)

                def onTimeout():
                    for i in range(1, 10):
                        time.sleep(0.5)
                        print(i)
                        self.label_3.setText(str(i))
                        QtGui.QGuiApplication.processEvents()
                onTimeout()
        Change_label()

    def retranslateUi(self, MainWindow):
        _translate = QtCore.QCoreApplication.translate
        MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
        self.pushButton.setText(_translate("MainWindow", "Start"))
        self.label_3.setText(_translate("MainWindow", "0"))
        self.pushButton_2.setText(_translate("MainWindow", "Stop"))

if __name__ == "__main__":
    import sys
    app = QtWidgets.QApplication(sys.argv)
    MainWindow = QtWidgets.QMainWindow()
    ui = Ui_MainWindow()
    ui.setupUi(MainWindow)
    MainWindow.show()
    sys.exit(app.exec_())

'''

和错误信息:

'''

Traceback (most recent call last):
File "D:/Analizy/23_Start_Star_PRE_with_GUI/GUI_files/GUI_star_only_push_button_and_label.py", line 76, in <module>
ui.setupUi(MainWindow)
File "D:/Analizy/23_Start_Star_PRE_with_GUI/GUI_files/GUI_star_only_push_button_and_label.py", line 12, in setupUi
self.process = QtCore.QProcess(self)
TypeError: QProcess(parent: QObject = None): argument 1 has unexpected type 'Ui_MainWindow'
Process finished with exit code 1

'''

0 个答案:

没有答案