Reputation: 36
I wrote a simulator for a microcontroller where I pass the results of the functions with events to the GUI. The problem is that the functions bound to the events never got called. I have the GUI and the simulator in two different threads.
'''Event class'''
NewRAMEvent = wx.NewEventType()
EVT_NEW_RAM = wx.PyEventBinder(NewRAMEvent, 1)
class RAM_Event(wx.PyCommandEvent):
def __init__(self, location = None, val = None):
wx.PyCommandEvent.__init__(self)
self._location = location
self._val = val
def SetVals(self, val, loc):
self._location = loc
self._val = val
def GetVal(self):
return self._val
def GetLocation(self):
return self._location
'''Creating event'''
evt1 = RAM_Event(0x03, uC.programCounter)
wx.CallAfter(recipient.ProcessEvent, evt1)
print("RAM event sent")
'''adding recipient to list and creating thread for simulator'''
simuThread = SimulatorThread()
simuThread.add_event_recipient(Myfrm)
'''GUI class'''
self.Bind(EVT_NEW_RAM, self.updateRAM)
def updateRAM(self, event):
print("received Event")
location = event.GetLocation()
value = event.GetVal()
print("value:", value)
RAM.writeToTable(self, location, value)
return
When I look at the console I see the message that it has been sent but not that it has been received.
Upvotes: 0
Views: 52
Reputation: 6206
Your RAM_Event
never sets its event type to NewRAMEvent
. You may want to take a look at the wx.lib.newevent
module, as it can take care of most of these details for you. You can create the new event type and event class with just a single line, like:
NewRAMEvent, EVT_NEW_RAM = wx.lib.newevent.NewCommandEvent()
The generated class will accept arbitrary keyword parameters which it will then assign as attributes of the class instance.
Also, wx.PostEvent(recipient, evt1)
would be a little better way to send the event than using the wx.CallAfter
. It's basically equivalent, but wx.PostEvent
is deigned for events and so it saves a few steps internally.
Upvotes: 1