如何在 pygtk 中使用线程

发布于 2024-07-18 15:36:18 字数 389 浏览 6 评论 0原文

我在 pygtk 中遇到线程问题。 我的应用程序包含一个从互联网下载图片然后用 pygtk 显示它的程序。 问题是,为了做到这一点并保持 GUI 响应能力,我需要使用线程。

因此,在用户单击“下载图片”按钮后,我进入回调,并调用该方法来下载同一类中的图片。

thread.start_new_thread(self.images_download, (path,pages)

这不起作用。我让程序进入线程的唯一方法是在启动任何线程之前使用

gtk.threads_init()

。现在它下载图片,但是GUI 仍然没有响应。 我用谷歌搜索了这个,并尝试将 gtk.threads_enter 和 gtk.threads_leave 放在线程周围,但它不起作用。

I have a problem with threads in pygtk. My application consist of a program that downloads pictures off the internet and then displays it with pygtk. The problem is that in order to do this and keep the GUI responsive, I need to use threads.

So I got into a callback after the user clicked on the button "Download pictures" and I call the method to download the pictures that is within that same class.

thread.start_new_thread(self.images_download, (path,pages)

This won't work. The only way I get my program to get into the thread is by using

gtk.threads_init()

Before starting any thread. Now it downloads the pictures but the GUI remains unresponsive.
I googled this and I tried putting gtk.threads_enter and gtk.threads_leave around the threads but it just doesn't work.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

匿名的好友 2024-07-25 15:36:18

你的问题有点模糊,如果没有参考你的实际代码,很难推测你做错了什么。

因此,我将为您提供一些阅读建议,然后根据经验进行大胆推测。

首先,您似乎认为只能通过使用线程来保持 GUI 的响应能力。 这不是真的。 您还可以异步编写代码,并在单线程应用程序中完成所有操作。 Twisted 就是建立在这个编程模型之上的。 我最近发表了一篇博客文章,解释了我如何创建异步任务接口,以及示例CLI 和 GTK+ 的运行程序。 您可以查看这些示例,了解如何异步实现任务,并且 UI 仍然会更新。

其次,如果您出于某种原因更喜欢使用线程,则需要稍微了解一下 GTK+ 线程模型。

您应该首先阅读有关该主题的 PyGTK 常见问题解答条目< /a>,您可能会找到这篇博文也很容易理解。

现在,开始猜测。 我猜测您正在尝试从线程更新 GTK UI,并且没有正确处理锁定。 如果是这种情况,您现在最好使用 gobject.idle_add() 将您想要执行的所有 UI 更新从线程推迟到主线程,这样,所有 UI 调用都将从主线程进行。 这是一种在编程中更容易遵循的心理模型。

一旦您觉得自己真正理解了线程和锁定模型,您就可以考虑从线程更新 UI,但很容易错过threads_enter()/threads_leave()

Your question is a bit vague, and without a reference to your actual code it's hard to speculate what you're doing wrong.

So I'll give you some pointers to read, then speculate wildly based on experience.

First of all, you seem to think that you can only keep the GUI responsive by using threads. This is not true. You can also write your code asynchronously, and do everything in a single-threaded application. Twisted is built on this programming model. I recently made a blog post that explains how I created an asynchronous task interface, and example runners both for CLI and GTK+. You can look at those examples to see how tasks can be implemented asynchronously, and the UI still gets updated.

Second, if you prefer to use threads for some reason, you will need to understand the GTK+ threading model a little.

You should start by reading The PyGTK FAQ entry on the subject, and you might find this blog post easy to understand too.

Now, on to speculation. I am guessing that you are trying to update your GTK UI from the thread, and not handling the locking properly. If this is the case, you are better off for now deferring all your UI updates you want to do from threads to the main thread by using gobject.idle_add() This way, all UI calls will be made from the main thread. It is an easier mental model to follow in your programming.

Once you feel you really understand the threading and locking models, you could consider updating the UI from your threads, but it's easy to miss a threads_enter()/threads_leave()

请爱~陌生人 2024-07-25 15:36:18

您可以使用 gtk.gdk.threads_init() 来允许任何线程使用相关的 gtk.gdk.threads_enter() 和 gtk.gdk.theads_leave() 锁修改 UI,但是,这样做的问题是,这并不在 Windows 上运行良好。 我已经在 Linux 上测试过它并且性能很好,但我没能让它在 win32 上运行。

===编辑===

我一直在浏览这个,你可以利用gobject.io_add_watch来检查你的套接字中是否有东西,抓住它然后更新GUI。 检查我的帖子:
没有线程的套接字(和其他一些文件)和 PyGTK。

You can use gtk.gdk.threads_init() in order to allow any thread modify the UI with the respecting gtk.gdk.threads_enter() and gtk.gdk.theads_leave() lock, but, the problem with this is that doesn't work well on windows. I have tested it on Linux and performs quite well, but I had no luck making this to work over win32.

=== Edit ===

I have been browsing about this, you could make use of the gobject.io_add_watch to check if there is something in your socket, grab it and then update the GUI. check my post about this:
Sockets (and some other files) and PyGTK without threads.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文