[英]Easy Multi-threading with PyQt5, for updating QTextBrowser contents
I have found stuff online suggesting that PyQt5 widgets are not thread safe. 我在网上发现了一些东西,表明PyQt5小部件不是线程安全的。 And other Stackoverflow answers suggest creating a class that only fits their problem.
其他Stackoverflow答案建议创建一个仅适合其问题的类。 I tried using
_thread
module in Python 3 which works for everything except PyQt. 我尝试在Python 3中使用
_thread
模块,该模块适用于除PyQt之外的所有内容。
app = QApplication([])
Ui_MainWindow, QtBaseClass = uic.loadUiType("UI/action_tab.ui") #specify the location of your .ui file
class MyApp(QMainWindow):
def __init__(self):
super(MyApp, self).__init__()
self.ui = Ui_MainWindow()
self.ui.setupUi(self)
self.threadPool = QThreadPool()
self.ui.queryBox.returnPressed.connect(self.load_response)
def start_loader(self):
self.loading_animate = QMovie('IMAGES/GIFS/load_resp.gif')
self.loading_animate.setScaledSize(QSize(400, 300))
self.ui.loader.setMovie(self.loading_animate)
self.loading_animate.setSpeed(200)
self.ui.loader.show()
self.loading_animate.start()
def stop_loader(self):
self.ui.loader.hide()
self.loading_animate.stop()
def get_response(self):
plain_text, speech = get_Wresponse(self.ui.queryBox.displayText())
self.stop_loader()
self.ui.textDisplay.setText(plain_text)
if speech == '':
say("Here you GO!")
else:
say(speech)
def load_response(self):
self.start_loader()
_thread.start_new_thread(self.get_response, ())
#self.get_response()
if __name__ == '__main__':
window = MyApp()
window.setWindowFlags(Qt.FramelessWindowHint)
window.show()
sys.exit(app.exec())
Error in above code follows, 上面代码中的错误如下,
QObject: Cannot create children for a parent that is in a different thread. (Parent is QTextDocument(0x19fe090b8c0), parent's thread is QThread(0x19fde197fb0), current thread is QThread(0x19fe3a0a5f0)
Do you think you can save me? 你认为你可以救我吗? Please Do !
请做! Thanks in Advance!!
提前致谢!!
You do not have to update the GUI from an external thread. 您不必从外部线程更新GUI。 There are several options like signals,
QMetaObject::invokeMethod(...)
, QEvent and QTimer::singleShot(0, ...)
with pyqtSlot. QMetaObject::invokeMethod(...)
有信号, QMetaObject::invokeMethod(...)
,QEvent和QTimer::singleShot(0, ...)
几种选项。
Using the last method the solution is as follows: 使用最后一种方法,解决方案如下:
from functools import partial
from PyQt5.QtCore import pyqtSlot
class MyApp(QMainWindow):
# ...
@pyqtSlot()
def stop_loader(self):
self.ui.loader.hide()
self.loading_animate.stop()
def get_response(self, text):
plain_text, speech = get_Wresponse(text)
QtCore.QTimer.singleShot(0, self.stop_loader)
wrapper = partial(self.ui.textDisplay.setText, plain_text)
QtCore.QTimer.singleShot(0, wrapper)
if speech == '':
say("Here you GO!")
else:
say(speech)
def load_response(self):
self.start_loader()
text = self.ui.queryBox.displayText()
_thread.start_new_thread(self.get_response, (text,))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.