python - 来自 Process 的简单 wxPython post 事件

标签 python user-interface process wxpython

我正在尝试创建一个从进程接收简单事件通知的窗口。这是我到目前为止的代码:

import wx, wx.lib.newevent, time, sys
from multiprocessing import Process

size_width = 320
size_height = 240
background_color = (226,223,206)

SomeNewEvent, EVT_SOME_NEW_EVENT = wx.lib.newevent.NewEvent()


class StatusWindow(wx.Frame):
    def __init__(self, parent):
        super(StatusWindow, self).__init__(parent, title='Monitor', size=(size_width, size_height))

        self.Bind(EVT_SOME_NEW_EVENT, self.updateStatus)

        staticBox = wx.StaticBox(self, label='Monitor Status:', pos=(5, 105), size=(size_width - 28, size_height/3))
        self.statusLabel = wx.StaticText(staticBox, label='None', pos=(10, 35), size=(size_width, 20), style=wx.ALIGN_LEFT)
        self.count = 0
        self.InitUI()

        self.monitor = cMonitor()
        self.monitor.start()

    def InitUI(self):
        panel = wx.Panel(self)    
        self.SetBackgroundColour(background_color)
        self.Centre()
        self.Show()

    def updateStatus(self, evt):
        self.statusLabel.SetLabel(evt.attr1)

class cMonitor(Process):
    def __init__(self):
        super(cMonitor, self).__init__()

    def run(self):
        time.sleep(2)
        print 'This is an update'
        #create the event
        evt = SomeNewEvent(attr1="Some event has just occured")
        #post the event
        wx.PostEvent(EVT_SOME_NEW_EVENT, evt)

if __name__ == '__main__':
    app = wx.App()
    window = StatusWindow(None)
    app.MainLoop()

窗口已创建,但进程似乎没有正确执行或发送事件后通知。我应该注意到 run 方法中的 print 语句也没有出现。是什么导致 GUI 不更新?这是我用作引用的内容:

http://wiki.wxpython.org/CustomEventClasses

最佳答案

首先,您的代码会抛出一个错误:

Traceback (most recent call last):
  File "C:\Python27\lib\multiprocessing\process.py", line 232, in _bootstrap
    self.run()
  File "C:\PyProgs\stackoverflow_answers\wx_answers\wx_events1.py", line 44, in run
    wx.PostEvent(EVT_SOME_NEW_EVENT, evt)
  File "C:\Python27\lib\site-packages\wx-3.0-msw\wx\_core.py", line 8410, in PostEvent
    return _core_.PostEvent(*args, **kwargs)
TypeError: in method 'PostEvent', expected argument 1 of type 'wxEvtHandler *'

根据docs , PostEvent(dest, event) 将事件发送到窗口或其他 wx.EvtHandler 以供稍后处理,但在您的代码中,第一个参数的类型为 PyEventBinder。您的代码必须如下所示:

wx.PostEvent(self.wxWindow, evt)

其中 self.wxWindow - StatusWindow 类的对象。但是还有另一个问题:你不能使用 wxPython 对象作为多处理器参数(link)。

做你想做的事情的一种方法 - 使用线程模块而不是多处理:

import wx, wx.lib.newevent, time, sys
from threading import *
size_width = 320
size_height = 240
background_color = (226,223,206)

SomeNewEvent, EVT_SOME_NEW_EVENT = wx.lib.newevent.NewEvent()


class StatusWindow(wx.Frame):
    def __init__(self, parent):
        super(StatusWindow, self).__init__(parent, title='Monitor', size=(size_width, size_height))

        self.Bind(EVT_SOME_NEW_EVENT, self.updateStatus)

        staticBox = wx.StaticBox(self, label='Monitor Status:', pos=(5, 105), size=(size_width - 28, size_height/3))
        self.statusLabel = wx.StaticText(staticBox, label='None', pos=(10, 35), size=(size_width, 20), style=wx.ALIGN_LEFT)
        self.count = 0
        self.InitUI()

        # Set up event handler for any worker thread results
        self.monitor = cMonitor(self)
        self.monitor.start()

    def InitUI(self):
        panel = wx.Panel(self)    
        self.SetBackgroundColour(background_color)
        self.Centre()
        self.Show()

    def updateStatus(self, evt):
        self.statusLabel.SetLabel(evt.attr1)



class cMonitor(Thread):
    def __init__(self, wxWindow):
        super(cMonitor, self).__init__()
        self.wxWindow = wxWindow


    def run(self):
        time.sleep(2)
        print 'This is an update'
        #create the event
        evt = SomeNewEvent(attr1="Some event has just occured")
        #post the event
        wx.PostEvent(self.wxWindow, evt)

if __name__ == '__main__':

    app = wx.App()
    window = StatusWindow(None)
    app.MainLoop()

关于python - 来自 Process 的简单 wxPython post 事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25703711/

相关文章:

python - Pandas 数据框包含列表

python - 从python在windows上查找程序的安装目录

java - 设置 swing JSlider 和 JTextField 的勾选行为

c++ - 与 sigsuspend() 的死锁

android - 本地(同一进程)服务是否需要 bindService?

python - 最近的最大/最小值

python - 我无法使用 Homebrew 软件安装 python,因为没有可用的瓶子

java - 无法将图像资源导入我们的 GUI

Java SWT - 如果新的 child 被添加到组合中,听听?

c# - 如何让c#程序按顺序运行?堆栈,自上而下