本文介绍了Pyqt:“动态地”从函数附加到qtextedit的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的pyqt gui中有一个按钮,当单击运行一个执行一些冗长的数学计算的函数。在这个功能里面有很多打印声明,如:

There is a button in my pyqt gui that when clicked runs a function that does some lengthy math calculations. Inside this function there were a lot of print statements like:

print "finished calculating task1 going on to task2"

所以通过使用这样的打印语句,我不需要让我们说一个进度条来表示程序进度。我在我的gui中添加了一个QTextEdit小部件,并用以下方式替换了该函数中的所有打印语句:

So by using print statements like that i didn't need to have let's say a progressbar for example to indicate program progress. I added a QTextEdit widget in my gui and replaced all print statements in that function with:

MyTextEdit.append('message')

其中 MyTextEdit 是一个QTextEdit小部件,消息是我想要打印功能的消息。

where MyTextEdit is a QTextEdit widget and message is the message i would like the function to print.

示例:

MyTextEdit.append('finished calculating task1 going on to task2')
task2 #lengthy second task
MyTextEdit.append('finished calculating task2 going on to task3')
task3 #lengthy third task

当我点击按钮并运行该函数时,该函数内的所有计算都完成,然后所有消息都附加到QTextEdit小部件。

When i click the button and the function runs, all calculations inside that function have to finish and then all messages are appended to the QTextEdit widget.

我以为每次 MyTextEdit.append('message')被执行,它将立即运行,小部件将在那个瞬间显示消息,而不是在所有其他消息的末尾显示。

I thought that each time a MyTextEdit.append('message') is executed it would run immediately and the widget would display the message at that very instant and not at the end along all other messages.

我做错了什么?

我想通过阅读这个

推荐答案

只需调用追加

你可以获得你的 QCoreApplication 的实例与静态方法 QCoreApplication.instance

You can get your instance of QCoreApplication with the static method QCoreApplication.instance

这将使Qt在完成正在执行的任务之前刷新您的gui,因为该命令将处理所有挂起的事件。

This will ask Qt to "refresh" your gui before finishing the tasks that are being executed, as the command processes all pending events.

这篇关于Pyqt:“动态地”从函数附加到qtextedit的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!