Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/318.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在Python pyqt4 GUI设计中使用线程_Python_Multithreading_Pyqt4 - Fatal编程技术网

如何在Python pyqt4 GUI设计中使用线程

如何在Python pyqt4 GUI设计中使用线程,python,multithreading,pyqt4,Python,Multithreading,Pyqt4,我已经编写了一个简单的PythonGUI代码,用于将代码休眠10秒钟。同时,GUI应该在不冻结的情况下运行。定义primari thready和secondary thread不可能吗?当我点击“运行”按钮时,pythonw.exe已经停止工作。有人对我如何解决这个问题有什么想法吗?多谢各位 import sys, threading from PyQt4 import QtGui, QtCore from PyQt4.QtCore import pyqtSlot

我已经编写了一个简单的PythonGUI代码,用于将代码休眠10秒钟。同时,GUI应该在不冻结的情况下运行。定义primari thready和secondary thread不可能吗?当我点击“运行”按钮时,pythonw.exe已经停止工作。有人对我如何解决这个问题有什么想法吗?多谢各位

    import sys, threading
    from PyQt4 import QtGui, QtCore
    from PyQt4.QtCore import pyqtSlot
    from PyQt4.QtGui import *

    class Window(QtGui.QMainWindow):
        def __init__(self):
            super(Window,self).__init__()
            self.setGeometry(400,150, 550, 500)
            self.setWindowTitle("my first program")
            self.home()

        def home(self):
            self.button_1 = QtGui.QPushButton('Run', self)
            self.button_1.resize(60,25)
            self.button_1.move(230,230)
            self.button_1.clicked.connect(self.Thread_test)

            self.textbox_1 = QTextEdit(self)
            self.textbox_1.move(15,290)
            self.textbox_1.resize(510,170)

            self.show()

        def Thread_test(self):
            t = threading.Thread(target=self.calculation)
            t.start()

        def calculation(self):
            msg="program is working"
            self.textbox_1.setText(msg)
            time.sleep(5)
            msg="program was slept for few sec."
            self.textbox_1.setText(msg)

    def run():
        app=QtGui.QApplication(sys.argv)
        GUI=Window()
        sys.exit(app.exec_())    
    run()

Qt小部件不是线程安全的。您应该仅从创建小部件的线程访问它。见:

所有小部件和几个相关类[…]都不能在辅助线程中工作

您必须通知主(GUI)线程以更新UI。您可以使用信号和插槽执行此操作