我试图创建一个窗口,从进程接收简单的事件通知。以下是我目前掌握的代码:
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不能更新??这是我用来作为参考的:
首先,您的代码抛出一个错误:
根据docs,}稍后处理,但在代码中,第一个参数的类型是
^{pr2}$PostEvent(dest, event)
发送一个事件到一个窗口或其他{PyEventBinder
。你的代码应该是这样的:其中
self.wxWindow
-类的对象。但是还有一个问题:不能将wxPython对象用作多处理器参数(link)。在一种方法是使用线程模块而不是多处理:
相关问题 更多 >
编程相关推荐