如何在pygtk中使用线程 [英] How to work with threads in pygtk

查看:78
本文介绍了如何在pygtk中使用线程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在pygtk中的线程有问题.我的应用程序包含一个程序,该程序可以从互联网上下载图片,然后使用pygtk进行显示.问题是,为了做到这一点并保持GUI响应,我需要使用线程.

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,(路径,页面)

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()

gtk.threads_init()

在启动任何线程之前.现在,它下载了图片,但GUI仍然没有响应. 我用谷歌搜索,并尝试将gtk.threads_enter和gtk.threads_leave放在线程周围,但这是行不通的.

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.

推荐答案

您的问题有点含糊,如果不参考您的实际代码,就很难推测您在做什么错.

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.

首先,您似乎认为只能使用线程来保持GUI响应.这不是真的.您还可以异步编写代码,并在单线程应用程序中执行所有操作. 扭曲是基于此编程模型构建的.最近,我撰写了一篇博客文章,其中介绍了我如何创建异步任务界面和示例CLI和GTK +都可以使用.您可以查看这些示例,以了解如何异步执行任务,并且UI仍得到更新.

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.

第二,如果您出于某种原因更喜欢使用线程,则需要稍微了解GTK +线程模型.

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

您应该先阅读 PyGTK常见问题解答条目,主题,您可能会发现此博客文章也很容易理解.

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

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

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.

一旦您真正了解线程和锁定模型,就可以考虑从线程中更新UI,但是很容易错过thread_enter()/threads_leave()

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()

这篇关于如何在pygtk中使用线程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆