无法从另一个线程停止计时器-移开焦点

问题描述 投票:0回答:1
import sys

from PyQt5.QtCore import QThread
from PyQt5.QtWidgets import QApplication, QWidget, QVBoxLayout, QLineEdit

class Worker(QThread):

    def __init__(self, textBox):
        super().__init__()
        self.textBox = textBox

    def run(self):
        while True:
            if self.textBox.text() == "close":
                app.quit()
                break

            if self.textBox.text() == "removeFocus":
                self.textBox.clearFocus()

class window(QWidget):
    def __init__(self):
        super().__init__()

        vBox = QVBoxLayout()
        self.setLayout(vBox)
        self.resize(600, 400)

        textBox = QLineEdit()
        vBox.addWidget(textBox)

        worker = Worker(textBox)
        worker.start()

        self.show()


if __name__ == "__main__":
    app = QApplication(sys.argv)
    window = window()
    sys.exit(app.exec())

我在文本框中键入“ 关闭”的效果很好,但是当我键入“ removeFocus”时,仍然可以起作用,但出现此错误:

QObject::killTimer: Timers cannot be stopped from another thread

即使程序正在运行,为什么还会出现这样的错误?

(由于我要执行的过程非常简单,所以我认为我无法详细介绍。我刚刚开始学习Python。这是我第一次使用此网站。很抱歉如果我在创建帖子时出错了,谢谢)

python multithreading pyqt5 qthread
1个回答
0
投票
© www.soinside.com 2019 - 2024. All rights reserved.