wxPython:线程GUI->使用自定义事件处理程序 [英] wxPython: Threading GUI --> Using Custom Event Handler

查看:200
本文介绍了wxPython:线程GUI->使用自定义事件处理程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试学习如何在主GUI应用程序上运行线程以进行串行端口发送/接收,同时保持我的GUI处于活动状态.我最好的Google搜索尝试使我进入了以下wxpython Wiki: http://wiki.wxpython.org/LongRunningTasks提供了几个示例.我决定学习第一个示例,涉及选择特定按钮时启动工作线程.

I am trying to learn how to run a thread off the main GUI app to do my serial port sending/receiving while keeping my GUI alive. My best Googling attempts have landed me at the wxpython wiki on: http://wiki.wxpython.org/LongRunningTasks which provides several examples. I have settled on learning the first example, involving starting a worker thread when the particular button is selected.

我无法理解自定义事件定义:

I am having trouble understanding the custom-event-definition:

def EVT_RESULT(win, func):
    """Define Result Event."""
    win.Connect(-1, -1, EVT_RESULT_ID, func)

class ResultEvent(wx.PyEvent):
    """Simple event to carry arbitrary result data."""
    def __init__(self, data):
        """Init Result Event."""
        wx.PyEvent.__init__(self)
        self.SetEventType(EVT_RESULT_ID)
        self.data = data

主要是

def EVT_RESULT(win, func):
    """Define Result Event."""
    win.Connect(-1, -1, EVT_RESULT_ID, func)

我认为EVT_RESULT放置在类之外,以使两个类都可以调用它(将其全局化?)

I think EVT_RESULT is placed outside the classes so as to make it call-able by both classes (making it global?)

然后..主GUI应用程序通过以下方式监视线程的进度:

And.. the main GUI app monitors the thread's progress via:

# Set up event handler for any worker thread results
EVT_RESULT(self,self.OnResult)

我还注意到,在很多示例中,当作者使用

I also notice that in a lot of examples, when the writer uses

from wx import *

他们只是通过以下方式绑定事物

they simply bind things by

EVT_SOME_NEW_EVENT(self, self.handler)

相对于

wx.Bind(EVT_SOME_NEW_EVENT, self.handler)

这并不能帮助我更快地理解它. 谢谢,

Which doesn't help me understand it any faster. Thanks,

推荐答案

您可以定义以下事件:

from wx.lib.newevent import NewEvent

ResultEvent, EVT_RESULT = NewEvent()

您可以这样发布事件:

wx.PostEvent(handler, ResultEvent(data=data))

像这样绑定它:

def OnResult(event):
    event.data

handler.Bind(EVT_RESULT, OnResult)

但是,如果您只需要从主线程中的非主线程进行调用,则可以使用wx.CallAfter

But if you just need to make a call from a non-main thread in the main thread you can use wx.CallAfter, here is an example.

当您不想硬编码谁负责什么时,自定义事件很有用(请参见观察者设计模式).例如,假设您有一个主窗口和几个子窗口.假设当主窗口中发生某些更改时,需要刷新某些子窗口.在这种情况下,主窗口可以直接刷新那些子窗口,但是更优雅的方法是定义一个自定义事件,并让主窗口将其发布到自身(而不必理会需要对此做出反应的人).然后,需要对该事件做出反应的子代可以通过绑定到自身来完成它(如果有多个子代,则调用event.Skip()以便调用所有绑定的方法很重要).

Custom events are useful when you don't want to hard code who is responsible for what (see the observer design pattern). For example, lets say you have a main window and a couple of child windows. Suppose that some of the child windows need to be refreshed when a certain change occurs in the main window. The main window could directly refresh those child windows in such a case but a more elegant approach would be to define a custom event and have the main window post it to itself (and not bother who needs to react to it). Then the children that need to react to that event can do it them selves by binding to it (and if there is more than one it is important that they call event.Skip() so that all of the bound methods get called).

这篇关于wxPython:线程GUI->使用自定义事件处理程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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