Hello all, I'm sorry if this has been answered but I couldn't find much about my particular situation. From my understanding most everything in wxPython inherits from the EvtHandler base class. So therefore everything has it's own event handler. What I want to know is if it's possible to replace everything with a single global event handler. Also, I have some network code and I would like to post events in response to receiving packets but none of my code is inherited from EvtHandler class. Is it possible to post events to wx without inheriting from EvtHandler? Thanks for the help.
Hello
all, I’m sorry if this has been answered but I couldn’t find much about
my particular situation. From my understanding most everything in
wxPython inherits from the EvtHandler base class. So therefore
everything has it’s own event handler. What I want to know is if it’s
possible to replace everything with a single global event handler.
Also, I have some network code and I would like to post events in
response to receiving packets but none of my code is inherited from
EvtHandler class. Is it possible to post events to wx without
inheriting from EvtHandler? Thanks for the help.
DM
Hi,
It may be that a good answer to your question might involve changing
the question… So, why do you need to implement a global event handler?
Yes, it is possible to post events to wx without inheriting from
EvtHandler. In general that would be where is an and is an such as a window. But it is difficult for me to suggest actual
code without understanding the actual problem that you are attempting
to solve. What kind of message to you want to send, and for what
purpose?
Hello all, I'm sorry if this has been answered but I couldn't find much about my particular situation. From my understanding most everything in wxPython inherits from the EvtHandler base class. So therefore everything has it's own event handler. What I want to know is if it's possible to replace everything with a single global event handler. Also, I have some network code and I would like to post events in response to receiving packets but none of my code is inherited from EvtHandler class. Is it possible to post events to wx without inheriting from EvtHandler? Thanks for the help.
DM
Hi,
It may be that a good answer to your question might involve changing the question...
So, why do you need to implement a global event handler?
Yes, it is possible to post events to wx without inheriting from EvtHandler. In general that would be *wx.PostEvent(w, evt)* where *w* is an *EvtHandler *and *evt *is an *Event *such as a window. But it is difficult for me to suggest actual code without understanding the actual problem that you are attempting to solve. What kind of message to you want to send, and for what purpose?
- Ken Seehart
The basic underlying structure of my program is like this. My network code sits in an infinite loop receiving packets. When a certain packet is received I want to trigger an event. That way each packet of interest will have it's own separate event and the rest of the app can bind to those events and respond to them. What I have tried to do up to now is create a new event handler that will be used for the network events. But I run into problems with I try and bind to my network event handler. This is the code I have so far in my attempt to accomplish this.
network.py - Contains all of my network code, in it I have the following:
ConnAcc_Evt_Class, EVT_CONNACC = NewEvent() # My custom event, used for when a connection is accepted
Net_Evt_Handler = wx.EvtHandler() #My attempt to create an event handler for the network code
#When I want to trigger the event I call:
evt = ConnAcc_Evt_Class()
wx.PostEvent(Net_Evt_Handler, evt)
widgets.py - Contains all of my derived widgets for my app
#This is where I attempt to bind a method to my custom network event #I don't really know what to put for /source/ since the source isn't a part of wx and doesn't have a GetID() method
network.Net_Evt_Handler.Bind(network.EVT_CONNACC, self.OnConnAcc, /source,/ wx.ID_ANY, wx.ID_ANY)
Perhaps it's just the fact that the hour's late, but I
don't understand what it is that you're bumping your
head against. If I was writing code like yours, I
would put in:
class NetworkEvent(wx.PyCommandEvent):
Type = wx.NewEventType()
def __init__(self):
wx.PyCommandEvent.__init__(self, self.Type,
-1)
EVT_NETWORK_EVENT = \
wx.PyEventBinder(NetworkEvent.Type, 1)
and then subclass it for each individual event I
needed to deal with, such as:
Note, I haven't done any testing with the above, but I
can't see why it wouldn't work.
Gre7g
···
--- Dustin Mitchell <dmmitche@purdue.edu> wrote:
The basic underlying structure of my program is like
this. My network
code sits in an infinite loop receiving packets.
When a certain packet
is received I want to trigger an event. That way
each packet of
interest will have it's own separate event and the
rest of the app can
bind to those events and respond to them. What I
have tried to do up to
now is create a new event handler that will be used
for the network
events. But I run into problems with I try and bind
to my network event
handler. This is the code I have so far in my
attempt to accomplish this.
____________________________________________________________________________________
We won't tell. Get more on shows you hate to love
(and love to hate): Yahoo! TV's Guilty Pleasures list. http://tv.yahoo.com/collections/265
The basic underlying structure of my program is like this. My network code sits in an infinite loop receiving packets. When a certain packet is received I want to trigger an event. That way each packet of interest will have it's own separate event and the rest of the app can bind to those events and respond to them. What I have tried to do up to now is create a new event handler that will be used for the network events. But I run into problems with I try and bind to my network event handler. This is the code I have so far in my attempt to accomplish this.
network.py - Contains all of my network code, in it I have the following:
ConnAcc_Evt_Class, EVT_CONNACC = NewEvent() # My custom event, used for when a connection is accepted
Net_Evt_Handler = wx.EvtHandler() #My attempt to create an event handler for the network code
#When I want to trigger the event I call:
evt = ConnAcc_Evt_Class()
wx.PostEvent(Net_Evt_Handler, evt)
wx.PostEvent adds the event to the pending event queue for the evthandler. The problem here I think is that the pending event queue is only processed for the app and window objects. Since your Net_Evt_Handler isn't the app or a window then it won't ever process the event. Is there any reason that you can't post the event to one of the windows in your app and then handle it there?
widgets.py - Contains all of my derived widgets for my app
#This is where I attempt to bind a method to my custom network event #I don't really know what to put for /source/ since the source isn't a part of wx and doesn't have a GetID() method
network.Net_Evt_Handler.Bind(network.EVT_CONNACC, self.OnConnAcc, /source,/ wx.ID_ANY, wx.ID_ANY)
The source parameter is just used to find an ID to use in the binding. That ID is later used for matching up events with handlers. For example if you have a panel with several buttons on it you would like to have a separate handler for each, so you make the bindings with EVT_BUTTON plus a source (or explicit ID). If the source or ID is left out then the same handler would be called for all buttons because the default is to match any ID in the incoming event. In your example above you are not setting the ID in the event object so it can't be matched to a specific source or ID anyway, so there is no reason to specify it in the Bind() call.
BTW, you should really take a look at the wx.lib.pubsub module as it provides an easier way to do what you are wanting to do. You just need to subscribe to message topics wherever you need to in your app, and then have the network code publish messages using those topics and all the plumbing in between is taken care of for you.
···
--
Robin Dunn
Software Craftsman http://wxPython.org Java give you jitters? Relax with wxPython!
Hello all, I'm sorry if this has been answered but I couldn't find much about my particular situation. From my understanding most everything in wxPython inherits from the EvtHandler base class. So therefore everything has it's own event handler. What I want to know is if it's possible to replace everything with a single global event handler. Also, I have some network code and I would like to post events in response to receiving packets but none of my code is inherited from EvtHandler class. Is it possible to post events to wx without inheriting from EvtHandler? Thanks for the help.
DM
Hi,
It may be that a good answer to your question might involve changing the question...
So, why do you need to implement a global event handler?
Yes, it is possible to post events to wx without inheriting from EvtHandler. In general that would be *wx.PostEvent(w, evt)* where *w* is an *EvtHandler *and *evt *is an *Event *such as a window. But it is difficult for me to suggest actual code without understanding the actual problem that you are attempting to solve. What kind of message to you want to send, and for what purpose?
- Ken Seehart
The basic underlying structure of my program is like this. My network code sits in an infinite loop receiving packets. When a certain packet is received I want to trigger an event. That way each packet of interest will have it's own separate event and the rest of the app can bind to those events and respond to them. What I have tried to do up to now is create a new event handler that will be used for the network events. But I run into problems with I try and bind to my network event handler. This is the code I have so far in my attempt to accomplish this.
network.py - Contains all of my network code, in it I have the following:
ConnAcc_Evt_Class, EVT_CONNACC = NewEvent() # My custom event, used for when a connection is accepted
Net_Evt_Handler = wx.EvtHandler() #My attempt to create an event handler for the network code
#When I want to trigger the event I call:
evt = ConnAcc_Evt_Class()
wx.PostEvent(Net_Evt_Handler, evt)
widgets.py - Contains all of my derived widgets for my app
#This is where I attempt to bind a method to my custom network event #I don't really know what to put for /source/ since the source isn't a part of wx and doesn't have a GetID() method
network.Net_Evt_Handler.Bind(network.EVT_CONNACC, self.OnConnAcc, /source,/ wx.ID_ANY, wx.ID_ANY)
Thanks for the help.
DM
I think I understand where you are at.
The trouble is that the wx application has it's own infinite loop. When you run your infinite loop, the application loop never gets a chance to run. If you want to have your own infinite loop, you have to do something to make it possible to have both loops running at the same time, and allow the network loop to communicate with the gui loop. There are various ways of doing this. The technique I am most familiar with is good old fashioned threads.
# this is untested pseudocode:
class NetworkConnection:
def _thread(self):
while connection is not closed:
x = read_packet
if x is an interesting packet:
wx.PostEvent(PacketEvent(x))
# Do Not Use SendEvent!
# and do not make any wx calls other than wx.PostEvent unless you know which ones are safe.
def open(self, host, port):
open the connection
self.thread = threading.Thread(target=self._thread)
self.thread.start()
Hello all, I'm sorry if this has been answered but I couldn't find much about my particular situation. From my understanding most everything in wxPython inherits from the EvtHandler base class. So therefore everything has it's own event handler. What I want to know is if it's possible to replace everything with a single global event handler. Also, I have some network code and I would like to post events in response to receiving packets but none of my code is inherited from EvtHandler class. Is it possible to post events to wx without inheriting from EvtHandler? Thanks for the help.
DM
---------------------------------------------------------------------
To unsubscribe, e-mail: wxPython-users-unsubscribe@lists.wxwidgets.org
For additional commands, e-mail: wxPython-users-help@lists.wxwidgets.org
Thanks for all of the help guys. I have figured it out.