Python PyQt5:如何在sub-tread中运行QTimer并将值返回到主窗口小部件?
我得说这是个初学者的问题。但是我做了很多尝试/搜索都没有成功 有一个Python PyQt5:如何在sub-tread中运行QTimer并将值返回到主窗口小部件?,python,pyqt,pyqt5,qthread,qtimer,Python,Pyqt,Pyqt5,Qthread,Qtimer,我得说这是个初学者的问题。但是我做了很多尝试/搜索都没有成功 有一个QLabel,通过运行代码,我希望将子线程中的值显示在它上面 另外,我希望将QTimer放在子线程中,因为时间是在那里控制的(而不是在主QThread中) 这就是我想要实现的QLable的效果 0 (show for 1 second) 1 (show for 1 second) 2 (show for 1 second) ... 11 (show for 2 second) 12 (show for 2 second) ...
QLabel
,通过运行代码,我希望将子线程中的值显示在它上面
另外,我希望将QTimer
放在子线程中,因为时间是在那里控制的(而不是在主QThread
中)
这就是我想要实现的QLable
的效果
0 (show for 1 second)
1 (show for 1 second)
2 (show for 1 second)
...
11 (show for 2 second)
12 (show for 2 second)
...
21 (show for 3 second)
22 (show for 3 second)
...
这是我的密码:
# -*- coding: utf-8 -*-
import sys
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
from PyQt5.QtCore import *
class WorkerThread(QThread):
def __init__(self, parent=None):
super(WorkerThread, self).__init__(parent)
self._running = False
self.timer = QTimer() # Question: define the QTimer here?
self.timer.timeout.connect(self.doWork)
self.timer.start(1000)
def run(self):
self._running = True
while self._running:
self.doWork()
def doWork(self):
for i in range(40): # Question: How to return the i onto the QLable?
if i <= 10:
# show the value of i on the QLabel and wait for 1 second
pass
elif i <= 20:
# show the value of i on the QLable and wait for 2 second
pass
elif i <= 30:
# show the value of i on the QLable and wait for 3 second
pass
else:
# show the value of i on the QLable and wait for 4 second
pass
def stop(self, wait = False):
self._running = False
if wait:
self.wait()
class MyApp(QWidget):
def __init__(self, parent= None):
super(MyApp, self).__init__(parent)
self.thread = WorkerThread()
self.initUI()
def initUI(self):
self.text = "hello world"
self.setGeometry(100, 100, 500, 100)
self.setWindowTitle('test')
self.lbl_1 = QLabel("for every number between 0-10 wait 1 second, for 11-20 wait 2 sec, ...", self)
self.show()
if __name__ == '__main__':
app = QApplication(sys.argv)
window = MyApp()
window.show()
sys.exit(app.exec_())
#-*-编码:utf-8-*-
导入系统
从PyQt5.QtWidgets导入*
从PyQt5.QtGui导入*
从PyQt5.QtCore导入*
类WorkerThread(QThread):
def uuu init uuu(self,parent=None):
super(WorkerThread,self)。\uuuu init\uuuu(父级)
self.\u running=False
self.timer=QTimer()#问题:在这里定义QTimer?
self.timer.timeout.connect(self.doWork)
自动定时器启动(1000)
def运行(自):
self.\u running=True
当self.\u运行时:
自我介绍
def doWork(自我):
对于范围(40)内的i:#问题:如何将i返回到QLable上?
如果我不需要使用QTimer
来执行所需的任务,那么在线程中可以使用阻塞元素,例如生成暂停的QThead::sleep()
。要发送信息,我们可以使用parent()
传递GUI,并将其与QMetaObject::invokeMethod()
相结合,我们可以根据Qt的规则更新标签:
class WorkerThread(QThread):
[...]
def doWork(self):
for i in range(40): # Question: How to return the i onto the QLable?
if i <= 10:
QThread.sleep(1)
elif i <= 20:
QThread.sleep(2)
elif i <= 30:
QThread.sleep(3)
else:
QThread.sleep(4)
gui = self.parent()
QMetaObject.invokeMethod(gui.lbl_1, "setText", Qt.QueuedConnection,
Q_ARG(str, str(i)))
[...]
class MyApp(QWidget):
def __init__(self, parent=None):
super(MyApp, self).__init__(parent)
self.thread = WorkerThread(self) # passing the window as a parent
self.thread.start()
self.initUI()
def initUI(self):
[...]
我们可以使用信号,而不是使用QMetaObject::invokeMethod
:
class WorkerThread(QThread):
numberChanged = pyqtSignal(str)
def __init__(self, parent=None):
[...]
def doWork(self):
for i in range(40): # Question: How to return the i onto the QLable?
if i <= 10:
interval = 1000
elif i <= 20:
interval = 2000
elif i <= 30:
interval = 3000
else:
interval = 4000
loop = QEventLoop()
QTimer.singleShot(interval, loop.quit)
loop.exec_()
self.numberChanged.emit(str(i))
[...]
class MyApp(QWidget):
def __init__(self, parent=None):
super(MyApp, self).__init__(parent)
self.thread = WorkerThread(self)
self.thread.start()
self.initUI()
def initUI(self):
self.text = "hello world"
self.setGeometry(100, 100, 500, 100)
self.setWindowTitle('test')
self.lbl_1 = QLabel("for every number between 0-10 wait 1 second, for 11-20 wait 2 sec, ...", self)
self.thread.numberChanged.connect(self.lbl_1.setText, Qt.QueuedConnection)
self.show()
class WorkerThread(QThread):
numberChanged=pyqtSignal(str)
def uuu init uuu(self,parent=None):
[...]
def doWork(自我):
对于范围(40)内的i:#问题:如何将i返回到QLable上?
如果我非常感谢你所有的三种方法!你帮了很多忙。你真是太好了!祝你在新的一年里万事如意!
class WorkerThread(QThread):
numberChanged = pyqtSignal(str)
def __init__(self, parent=None):
[...]
def doWork(self):
for i in range(40): # Question: How to return the i onto the QLable?
if i <= 10:
interval = 1000
elif i <= 20:
interval = 2000
elif i <= 30:
interval = 3000
else:
interval = 4000
loop = QEventLoop()
QTimer.singleShot(interval, loop.quit)
loop.exec_()
self.numberChanged.emit(str(i))
[...]
class MyApp(QWidget):
def __init__(self, parent=None):
super(MyApp, self).__init__(parent)
self.thread = WorkerThread(self)
self.thread.start()
self.initUI()
def initUI(self):
self.text = "hello world"
self.setGeometry(100, 100, 500, 100)
self.setWindowTitle('test')
self.lbl_1 = QLabel("for every number between 0-10 wait 1 second, for 11-20 wait 2 sec, ...", self)
self.thread.numberChanged.connect(self.lbl_1.setText, Qt.QueuedConnection)
self.show()