我正在尝试使基本功能
在按下“开始”按钮启动计数器后,在按下“停止”按钮停止计数器后,
但在我开始处理后,看起来只有计数线程在工作,无法按停止按钮
#!/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_())
谢谢
最佳答案
现在,您甚至在创建线程之前就调用了慢函数。尝试以下方法:
thread = Thread(target=self.counterr)
thread.start()
在Qt应用程序中,您可能还考虑了
QThread
类,该类可以运行自己的事件循环并使用信号和插槽与您的主线程进行通信。关于python - python GUI卡住,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/12911136/