gpt4 book ai didi

python 用户界面卡住

转载 作者:太空宇宙 更新时间:2023-11-03 13:49:14 24 4
gpt4 key购买 nike

我正在尝试实现基本功能按下“开始”按钮后开始计时,按下停止按钮后停止计时,但是在我开始处理之后,看起来只有计数线程在工作并且无法按下停止按钮

#!/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 用户界面卡住,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12911136/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com