哪些 GUI 框架最适合多线程 Python 程序?
我正在编写一个带有 GUI 的多线程 Python 程序,其中有几个模块可以通过更改文本和背景颜色来“触摸”GUI。我目前正在使用 PyGTK,发现 GUI 有时会“无提示”崩溃(没有错误消息;程序只是终止),有时会遇到分段错误。
这个网站指出 GTK 并不是完全线程-安全,而且 PyGTK 多线程编程很棘手。是否有更好的 Python GUI 框架用于多线程程序且不太可能产生问题?
I am writing a multi-threaded Python program with a GUI, with several modules that "touch" the GUI by changing text and background colors. I am currently using PyGTK and am finding that the GUI sometimes crashes "silently" (no error messages; the program just terminates), and sometimes encounters segmentation faults.
This site notes that GTK is not completely thread-safe, and that PyGTK multi-threaded programming is tricky. Are there better Python GUI frameworks for multi-threaded programs that are less likely to produce problems?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
哦,我绝对推荐 PyQt4。起初,我没有明白这些
SIGNAL
和EMIT
的废话,但现在我已经用它编写了一个程序,QThread
模块非常有用。至于稳定性,我从来没有遇到过崩溃。即使当我调试半功能代码时,QT 也没有任何问题。每当我单击带有无效信号槽的按钮时,它都会向控制台窗口抛出错误。
另一方面,GTK 只是“偶尔爆发一次,没有任何错误”。只是您极具描述性和友好的
分段错误
。这是我觉得使用 PyQt 很愉快的原因之一。当你遇到错误时,你实际上知道出了什么问题。我很确定这只是个人喜好,但还有一个优点是 Mac、Linux 和 Windows 上具有原生外观的 GUI。 Windows 上的 GTK+(不要误会我的意思。我使用 Ubuntu)只是有一种 X-org 的感觉,这让我感到不安。
祝你好运!
为了让 PyQt 更有吸引力,这里是我的书籍装订应用程序的摘录(有点乱):
Ohh, I definitely recommend PyQt4. At first, I didn't get all this
SIGNAL
andEMIT
nonsense, but now that I've made a program with it, theQThread
module is amazingly useful.As for stability, I have never had a crash, ever. Even while I was debugging half-functional code, QT didn't have any problems. It just threw an error to the console window whenever I clicked a button with an invalid signal slot.
GTK, on the other hand, just 'sploded once in a while with no errors whatsoever. Just your extremely descriptive and friendly
Segmentation Fault
. That was one of the reasons I find PyQt a joy to work with. When you get an error, you actually know what's wrong.I'm pretty sure it's personal preference after that, but one more plus is native-looking GUIs on Mac, Linux, and Windows. GTK+ on Windows (don't get me wrong. I use Ubuntu) just has this X-org feel to it, which disturbs me.
Good luck!
Just to make PyQt a bit more attractive, here's an excerpt from my book binding application (it's a bit messy):
如果您要从线程更新 GUI,您可能需要使用 gobject.idle_add() 以便稍后在循环中调用 GUI 更新函数,大多数 GUI 框架(如 Qt)要求您添加一个稍后在主循环空闲时调用的回调。 GTK 还支持通过使用 gtk.gdk.lock 上下文管理器或调用 gtk.gdk.threads_enter 和 gtk.gdk.threads_leave 从线程调用 GUI 函数/code> 围绕你的 GUI 调用。
所以你要么这样做:
要么你这样做:
If you are updating the GUI from a thread, you might want to use
gobject.idle_add()
so that the GUI update function is called later in the loop, most GUI frameworks (like Qt) require you to add a callback that will be called later when the mainloop is idle. GTK also supports calling the GUI functions from threads by using thegtk.gdk.lock
context manager or callinggtk.gdk.threads_enter
andgtk.gdk.threads_leave
around your GUI calls.So you either do:
Or you do: