有以下代码:
import time
import sys
#from PyQt6.QtWidgets import (
from PyQt5.QtWidgets import (
QApplication,
QPushButton,
QTextEdit,
QVBoxLayout,
QWidget
)
from threading import Thread
class Window(QWidget):
def __init__(self):
super().__init__()
mainLayout = QVBoxLayout()
self.output = QTextEdit()
self.output.setReadOnly(True)
mainLayout.addWidget(self.output)
self.startButton = QPushButton("Start")
self.startButton.clicked.connect(self.start)
mainLayout.addWidget(self.startButton)
self.setLayout(mainLayout)
def task(self):
while True:
obj = Actions()
obj.do()
time.sleep(3)
def start(self):
t1 = Thread(target=self.task)
t1.start()
class Actions():
def do(self):
window.output.append("...")
if __name__ == "__main__":
app = QApplication([])
window = Window()
window.show()
sys.exit(app.exec())
执行此示例时(针对PyQt5)我收到以下计划的警告:
QObject::connect:无法对类型为“QTextCursor”的参数进行排队(确保使用 qRegisterMetaType() 注册了“QTextCursor”。)
- 您无法与其他线程中的小部件交互,因此会显示此警告
- 当一个线程需要改变窗口中的某些东西时,它向主线程发送一个信号,主线程中的处理程序执行必要的改变
如何落实第二点?有没有关于这个主题的例子的好教程?
先感谢您!
您不需要额外的库来组织流工作。
阅读信号和插槽
我们研究了3 种不同的简单方法来处理流。