PyQT 在调用其他函数之前强制更新 textEdit [英] PyQT force update textEdit before calling other function
问题描述
我的问题是关于 PyQT5.我尝试创建一个带有按钮的对话框窗口,单击该按钮时
My question concerns PyQT5. I try to have a dialog window with a button that when clicked
- 更新
QTextEdit
字段的一些文本 - 调用一个函数(需要很长时间才能终止)
像这样:
class StartDialog(QtWidgets.QWidget, start_dialog_ui.Ui_Dialog):
def __init__(self, parent):
super(self.__class__, self).__init__()
self.setupUi(self)
self.OKButton.clicked.connect(self.start)
def start(self):
self.startDialogTextEdit.append("simulation running ...")
run_lengthy_function(self)
但是,当我运行 GUI 时,我注意到文本仅在 冗长函数终止之后更新,尽管 QTextEdit.append
在冗长函数之前调用.如何强制提前更新文本?
However, when I run my GUI I notice that the text is updated only after the lengthy function has terminated, although the QTextEdit.append
is called before the lengthy function. How can I enforce that the text is updated in advance?
到目前为止我尝试过的(但没有奏效)是让 Python 在触发冗长的函数调用之前等待一段时间,即
What I tried so far (but didn't work) was to let Python wait some time before triggering the lengthy function call, i.e.
from time import sleep
class StartDialog(QtWidgets.QWidget, start_dialog_ui.Ui_Dialog):
def __init__(self, parent):
super(self.__class__, self).__init__()
self.setupUi(self)
self.OKButton.clicked.connect(self.start)
def start(self):
self.startDialogTextEdit.append("simulation running ...")
sleep(5)
run_lengthy_function(self)
推荐答案
重绘在事件循环中被调用,因此整个线程休眠不会改变任何东西.
The repaint is called in event loop so sleep the whole thread does not change anything.
您可以通过以下方式手动调用重绘:
You can call repaint manually by:
self.startDialogTextEdit.repaint()
或调用静态方法:
QCoreApplication.processEvents()
在内部也调用重绘
这篇关于PyQT 在调用其他函数之前强制更新 textEdit的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!