sal*_*ala 3 python user-interface multithreading
在按下"开始"按钮启动计数器后按下停止按钮停止计数器后,我正在尝试制作基本功能,但是在我开始处理之后,它看起来只是计数线程正在工作,并且无法按下停止按钮
#!/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_())
Run Code Online (Sandbox Code Playgroud)
谢谢
现在,在创建线程之前调用slow函数.试试这个:
thread = Thread(target=self.counterr)
thread.start()
Run Code Online (Sandbox Code Playgroud)
在Qt应用程序中,您可能还会考虑QThread可以运行自己的事件循环并使用信号和插槽与主线程进行通信的类.