2015-10-12 81 views
1

我是使用Python上的线程的新用户,请在此我需要帮助。Python - 使用PyQt进行线程编程

我使用PyQt的,当我使用一个循环,主窗口被冻结,直到循环结束。

我读到螺纹,蟒蛇,它似乎一个解决方案,但我不知道,如果使用线程的,我对我的代码写的井。

这是我的代码的例子。

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_()) 

非常感谢!!

回答

1

您即将加入该线程。如果你想要它在后台运行,然后删除线

t1.join() 
+0

感谢pacholik!它的工作! :) –