关于python:如何将函数连接到主线程外的PyQt信号
How to connect functions to PyQt signals outside of main thread
我正在创建一个 PyQt 应用程序,我希望有一个后台线程来连接一些事件处理程序,然后永远循环直到主窗口关闭。我遇到的问题是,我连接的事件处理程序只有在它们是我的
|
1
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
import threading
from PyQt5.QtWidgets import QApplication, QDialog, QPushButton, QVBoxLayout class MainWindow(QDialog): self.button1 = QPushButton("Click Me", self) layout = QVBoxLayout() def test(self): def test2(): def main(window):
app = QApplication([]) |
当我运行此代码时,第一个按钮按预期向控制台打印一条消息,但第二个按钮在单击时什么也不做。如果我在主线程中运行
我仍在找出问题的原因,但解决方案是指出连接的类型,在这种情况下
|
1
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
import threading
from PyQt5 import QtCore, QtWidgets class MainWindow(QtWidgets.QDialog): @QtCore.pyqtSlot() def test2(): def main(window): if __name__ == '__main__': |
相关讨论
- 谢谢,使用
Qt.DirectConnection 解决了这个问题。