计时器不能从另一个线程停止 - 移除焦点
Timers cannot be stopped from another thread - Remove Focus
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())
当我在文本框中键入“close”时它工作得很好但是当我键入“removeFocus”时它仍然有效但是我收到此错误:
QObject::killTimer: Timers cannot be stopped from another thread
为什么我的程序是 运行 却出现这样的错误?
(由于我想做的过程很简单,我想我不能说的很详细,刚开始学习Python,这是第一个我使用这个网站的时间。如果我在创建 post 时犯了错误,我很抱歉。谢谢)
在 Qt 中,您不能从另一个线程访问或修改 GUI 信息(参见 this for more information) since it does not guarantee that it works (the GUI elements are not thread-safe),幸运的是,您的情况没有问题,但实际使用您的方法是危险的。
在你的情况下,也没有必要使用线程,因为使用来自 QLineEdit 的 textChanged 信号就足够了。
import sys
from PyQt5.QtCore import pyqtSlot
from PyQt5.QtWidgets import QApplication, QWidget, QVBoxLayout, QLineEdit
class Window(QWidget):
def __init__(self):
super().__init__()
vBox = QVBoxLayout(self)
self.resize(600, 400)
self.textBox = QLineEdit()
vBox.addWidget(self.textBox)
self.textBox.textChanged.connect(self.on_text_changed)
@pyqtSlot(str)
def on_text_changed(self, text):
if text == "close":
QApplication.quit()
elif text == "removeFocus":
self.textBox.clearFocus()
if __name__ == "__main__":
app = QApplication(sys.argv)
window = Window()
window.show()
sys.exit(app.exec())
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())
当我在文本框中键入“close”时它工作得很好但是当我键入“removeFocus”时它仍然有效但是我收到此错误:
QObject::killTimer: Timers cannot be stopped from another thread
为什么我的程序是 运行 却出现这样的错误?
(由于我想做的过程很简单,我想我不能说的很详细,刚开始学习Python,这是第一个我使用这个网站的时间。如果我在创建 post 时犯了错误,我很抱歉。谢谢)
在 Qt 中,您不能从另一个线程访问或修改 GUI 信息(参见 this for more information) since it does not guarantee that it works (the GUI elements are not thread-safe),幸运的是,您的情况没有问题,但实际使用您的方法是危险的。
在你的情况下,也没有必要使用线程,因为使用来自 QLineEdit 的 textChanged 信号就足够了。
import sys
from PyQt5.QtCore import pyqtSlot
from PyQt5.QtWidgets import QApplication, QWidget, QVBoxLayout, QLineEdit
class Window(QWidget):
def __init__(self):
super().__init__()
vBox = QVBoxLayout(self)
self.resize(600, 400)
self.textBox = QLineEdit()
vBox.addWidget(self.textBox)
self.textBox.textChanged.connect(self.on_text_changed)
@pyqtSlot(str)
def on_text_changed(self, text):
if text == "close":
QApplication.quit()
elif text == "removeFocus":
self.textBox.clearFocus()
if __name__ == "__main__":
app = QApplication(sys.argv)
window = Window()
window.show()
sys.exit(app.exec())