wxpython - 线程和窗口事件 [英] wxpython -- threads and window events

查看:254
本文介绍了wxpython - 线程和窗口事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个wxPython应用程序( http://www.OpenSTV.org ),使用方法对选票进行计数有多轮。我想做两件事:



(1)对于大量的选票,这可能有点慢,所以我想显示用户一个进度对话框,所以他不认为应用程序被冻结。



(2)我想允许用户手动破解关系,这需要计数代码显示一个对话框窗口。



要实现(1),我创建一个线程来运行计数代码,这样可以让我向用户。



然而,问题在于计数代码不是主线程,只有wxPython中的主线程才能处理窗口事件。



我想我可以创建一个线程来运行进度对话框,但这似乎很尴尬。有没有更好的方法来实现(1)和(2)?

解决方案

使用队列在线程之间进行通信和同步,每个线程都拥有,并且与不方便共享的资源进行专门的交互。 / p>

在只有主线程可以真正处理GUI的GUI工具包中,主线程应该沿着设置并启动执行实际工作的线程,然后不做任何操作但是GUI工作,使用队列与其他线程进行通信。



对于(1),当你的计数线程有更新时,它应该它放到主线程等待的队列中;当您的主线程在该队列上获得适当的消息时,它会更新进度对话框。



对于(2),计数线程发送让用户断开请求,主线程得到并作出适当的响应,并在单独的队列中发回分辨率。



所以一般来说,有两种通信不需要回应,还有其他的。对于前者,只要把通知放在适当的队列上,并且继续进行 - 它将在适当的时候采取行动。对于后一种,我最喜欢的成语是将一个对(请求,response_queue)放在适当的队列上。如果否则相同的请求有所不同,有些需要响应,而其他则不需要,当不需要响应时排队(请求,无)(和(请求,q)当需要响应时,q的队列)是一个很好的,容易的,和普通成语。


I have a wxPython application (http://www.OpenSTV.org) that counts ballots using methods that have multiple rounds. I'd like to do two things:

(1) For a large number of ballots, this can be a bit slow, so I'd like to show the user a progress dialog so he doesn't think the application is frozen.

(2) I'd like to allow the user to break ties manually, and this requires the counting code to show a dialog window.

To achieve (1), I create a thread to run the counting code, and this allows me to present a nice progress dialog to the user.

The problem with this, however, is that the counting code is not the main thread, and only the main thread in wxPython can process window events.

I suppose I could create a thread to run the progress dialog instead, but this seems awkward. Is there a better way of accomplishing both (1) and (2)?

解决方案

Use Queue to communicate and synchronize among threads, with each thread "owning" and exclusively interacting with a resource that's not handy to share.

In GUI toolkits where only the main thread can really handle the GUI, the main thread should play along -- set up and start the threads doing the actual work, then do nothing but GUI work, using Queues to communicate to and from the other threads.

For (1), when your counting thread has an update, it should put it to the Queue where the main thread is waiting; when your main thread gets a suitable message on that Queue, it updates the progress dialog.

For (2), the counting thread sends the "have the user break a tie" request, main thread gets it and responds appropriately, and sends back the resolution on a separate Queue.

So in general, there are two kinds of communications: one that don't require a response, and others that do. For the former kind, just put the notification on the appropriate queue and simply proceed -- it will be acted on in due course. For the latter kind, my favorite idiom is to put on the appropriate queue a pair (request, response_queue). If otherwise identical requests differ in that some need a response and others don't, queueing (request, None) when no response is needed (and (request, q) where q's a Queue when a response IS needed) is a nice, easy, and general idiom, too.

这篇关于wxpython - 线程和窗口事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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