Skip to main content
 首页 » 编程设计

Python之使用 PyQt 进行线程处理

2024年10月24日9think

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

非常感谢!!

请您参考如下方法:

您将立即加入主题。如果您希望它在后台运行,请删除该行

t1.join()