python用户界面冻结
我正在尝试做一些基本的功能 按下“启动”按钮启动计数器后,按下停止按钮停止计数器后, 但在我启动进程之后,看起来只有计数线程在工作,不可能按下停止按钮python用户界面冻结,python,multithreading,user-interface,Python,Multithreading,User Interface,我正在尝试做一些基本的功能 按下“启动”按钮启动计数器后,按下停止按钮停止计数器后, 但在我启动进程之后,看起来只有计数线程在工作,不可能按下停止按钮 #!/usr/bin/python # -*- coding: utf-8 -*- import sys from PyQt4 import QtGui, QtCore from test.test_sax import start import time from threading import Thread import threadin
#!/usr/bin/python
# -*- coding: utf-8 -*-
import sys
from PyQt4 import QtGui, QtCore
from test.test_sax import start
import time
from threading import Thread
import threading
class Example(QtGui.QWidget):
x = 1
bol = True
def __init__(self):
super(Example, self).__init__()
self.qbtn = QtGui.QPushButton('Quit', self)
self.qbtn.resize(self.qbtn.sizeHint())
self.qbtn.move(50, 50)
self.qbtn2 = QtGui.QPushButton('Start', self)
self.qbtn2.resize(self.qbtn2.sizeHint())
self.qbtn2.move(150, 50)
self.qbtn.clicked.connect(self.stopCounter)
self.qbtn2.clicked.connect(self.startUI)
self.setGeometry(300, 300, 250, 150)
self.setWindowTitle('Quit button')
self.show()
def stopCounter(self):
Example.bol = False
def startUI(self):
Example.bol = True
thread = Thread(self.counterr())
def counterr(self):
x = 0
while Example.bol:
print x
x += 1
if __name__ == '__main__':
app = QtGui.QApplication(sys.argv)
a = Example()
sys.exit(app.exec_())
thx现在在创建线程之前调用slow函数。请尝试以下方法:
thread = Thread(target=self.counterr)
thread.start()
在Qt应用程序中,您也可以考虑可以运行自己的事件循环并使用信号和槽与主线程通信的类。 ,您完全不正确地使用<代码>线程< /代码>类。您正在向它传递
counter r
方法的结果,该方法永远不会返回
将计数器
(不调用它)作为目标传递给线程
类,然后显式启动它:
def startUI(self):
self.bol = True
thread = Thread(target=self.counterr)
thread.start()
另外,只需作为实例变量而不是类变量访问bol
。试试这个:您必须导入线程并开始它,然后编写这个。线程。开始新线程(self.counter,(0,))