A question about event handlers with wxPython

E

Erik Lind

I'd appreciate any pointer on a simple way to tell within an event handler
where the event came from.
I want to have "while" condition in a handler to stop or change processing
if an event occurs from some other button click.

Trying to bind more than one event to the same handler still doesn't tell me
where the event came from in a basic bind. Is there an argument I can put in
the bind so as to identify the source of the event in the event argument? .
 
M

Miki

Hello Eric,
I'd appreciate any pointer on a simple way to tell within an event handler
where the event came from.
def HandleSomething(self, event):
generating_control = event.GetEventObject()
print generating_control

HTH,
 
E

Erik Lind

def HandleSomething(self, event):
generating_control = event.GetEventObject()
print generating_control

HTH,

Thank you.That is what I was looking for, but as often seems the case, one
thing exposes another. Is there any way to listen for events without
specifically binding to a handler (it seems one cannot bind an event to two
handlers?)? One could do so with globals, but I'm trying to avoid that.

For example, "press any button to stop"


def HandleSomething(self, event):
.................
while generating_control: == something:
run
else
stop
 
M

Mike Driscoll

Thank you.That is what I was looking for, but as often seems the case, one
thing exposes another. Is there any way to listen for events without
specifically binding to a handler (it seems one cannot bind an event to two
handlers?)? One could do so with globals, but I'm trying to avoid that.

For example, "press any button to stop"

def HandleSomething(self, event):
.................
while generating_control: == something:
run
else
stop

There are a number of ways to handle this. You could just bind the
parent to the handler. Something like this:

self.Bind(wx.EVT_BUTTON, self.onStop)

This will bind all button presses to the onStop handler. You could
also do something like this:

self.Bind(wx.EVT_BUTTON, self.onBtnStop)

def onBtnStop(self, event):
#do something
event.Skip()

By calling the Skip() method, it will propagate the event and look for
another handler, which in this case would be the onStop handler. On
Windows, you will most likely need to make a wx.Panel be the parent of
the rest of the widgets to have this effect.

My complete test code is below.

<code>

import wx

class Closer(wx.Frame):
def __init__(self):
wx.Frame.__init__(self, None, wx.ID_ANY, title='Test Frame')
panel = wx.Panel(self, -1)

sizer = wx.BoxSizer(wx.VERTICAL)
btn1 = wx.Button(panel, wx.ID_ANY, 'Button 1')
btn2 = wx.Button(panel, wx.ID_ANY, 'Button 2')
btn3 = wx.Button(panel, wx.ID_ANY, 'Button 3')

sizer.Add(btn1)
sizer.Add(btn2)
sizer.Add(btn3)

self.Bind(wx.EVT_BUTTON, self.onDone)
self.Bind(wx.EVT_BUTTON, self.onStop, btn1)
panel.SetSizer(sizer)

def onStop(self, event):
print 'Stop!'
event.Skip()

def onDone(self, event):
print 'Done!'

if __name__ == '__main__':
app = wx.PySimpleApp()
Closer().Show()
app.MainLoop()

</code>

FYI: There is an excellent wxPython group that you can join over on
the wxPython.org website.

Mike
 
M

Mike Driscoll

Thank you.That is what I was looking for, but as often seems the case, one
thing exposes another. Is there any way to listen for events without
specifically binding to a handler (it seems one cannot bind an event to two
handlers?)? One could do so with globals, but I'm trying to avoid that.

For example, "press any button to stop"

def HandleSomething(self, event):
.................
while generating_control: == something:
run
else
stop

I forgot to provide a link to a fairly straight-forward explanation of
event propagation on the wxPython wiki: http://wiki.wxpython.org/EventPropagation

Hope that helps!

Mike
 
E

Erik Lind

That all looks cool. I will experiment more. I'm a bit slow on this as only
two weeks old so far.

Thanks for the patience
 
M

Mike Driscoll

That all looks cool. I will experiment more. I'm a bit slow on this as only
two weeks old so far.

Thanks for the patience

No problem. I'm pretty slow with some toolkits too...such as
SQLAlchemy. Ugh.

Mike
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,777
Messages
2,569,604
Members
45,217
Latest member
topweb3twitterchannels

Latest Threads

Top