如何开始“绘图循环”在 PyQt 中?
很多时候,当我们绘制 GUI 时,我们希望 GUI 根据程序中的数据变化进行更新。在程序开始时,假设我已经根据初始数据绘制了 GUI。这些数据会不断变化,那么我怎样才能不断地重绘我的GUI呢?
Often times when we're drawing a GUI, we want our GUI to update based on the data changing in our program. At the start of the program, let's say I've drawn my GUI based on my initial data. That data will be changing constantly, so how can I redraw my GUI constantly?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我发现做到这一点的最好方法是在 QThread 中运行核心程序并使用信号与 GUI 进行通信。这是一个示例,我在主程序执行一些操作时更新进度对话框。
这是我正在开发的一个项目的代码摘录。基本思想是,我将许多文件添加到库对象中,并在添加文件时更新进度。
该操作由
Library
类启动。执行实际工作的步骤位于AddFilesThread
中。让我知道这是否有帮助。如果您需要,我可以尝试整理一个工作示例而不是代码摘录。
如果您想查看我使用的完整代码,请转到此处: hystrix_library.py。我使用的diaglog 类位于该文件中。我不能说这一定是最好的做事方式,但它效果很好并且相当容易阅读。
The best way that I have found to do this is to run your core program in a QThread and use signals to communicate with your gui. Here is an example where I update a progress dialog as my main program does some stuff.
Here is a code excerpt from a project that I was working on. The basic idea is that I am adding a number of files to a library object and updating the progress as the files are added.
The action is started by the
Library
class. The tread that does the actual work is in theAddFilesThread
.Let me know if this is helpful. If you need I can try to put together a working example instead of a code excerpt.
If you want to see the full code that I used go here: hystrix_library.py. The diaglog class that I used is in that file. I can't say that this is necessarily the best way to do things, but it works well and is fairly easy to read.
您可以创建一个线程来不断更新 GUI,只需将需要更新的图形小部件的引用传递给它即可
You could create a thread to update the GUI constantly, just pass to it references to the graphical widgets that need to be updated