使用PyQ的Python线程

2024-03-29 00:30:02 发布

您现在位置:Python中文网/ 问答频道 /正文

我对Python上线程的使用是新的,请,我需要这方面的帮助。你知道吗

我使用的是PyQt,当我使用循环时,主窗口被冻结,直到循环完成。你知道吗

我读过python上的线程技术,它似乎是一个解决方案,但我不知道我在代码中编写的线程技术是否有用。你知道吗

这是我的代码的一个例子。你知道吗

from Window import *
import sys, threading

class Window(QtGui.QDialog):
    def __init__(self, parent=None):
        QtGui.QWidget.__init__(self, parent)
        self.ui = Ui_Window()
        self.ui.setupUi(self)
        QtCore.QObject.connect(self.ui.button_download,   QtCore.SIGNAL('clicked()'), start)

print("I'm the main thread")

def start():
    t1 = threading.Thread(target=process)
    t1.start()
    t1.join()

def process():
    for i in range(0, 1000):
        print("I'm the thread:", i)

if __name__ == "__main__":
    app = QtGui.QApplication(sys.argv)
    myapp = Window()
    myapp.show()
    sys.exit(app.exec_())

非常感谢!!你知道吗


Tags: 代码importselfuiinitdefsyswindow