[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.python

A question about event handlers with wxPython

Erik Lind

1/15/2008 4:50:00 AM

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? .





8 Answers

Miki

1/15/2008 6:37:00 AM

0

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,
--
Miki Tebeka <miki.tebeka@gmail.com>
http://pythonwise.bl...

Erik Lind

1/15/2008 3:04:00 PM

0

> 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


Mike Driscoll

1/15/2008 3:22:00 PM

0

On Jan 15, 9:04 am, "Erik Lind" <el...@spamcop.net> wrote:
> > 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

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

Mike Driscoll

1/15/2008 4:49:00 PM

0

On Jan 15, 9:04 am, "Erik Lind" <el...@spamcop.net> wrote:
> > 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

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

Hope that helps!

Mike

Erik Lind

1/15/2008 8:20:00 PM

0

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


Mike Driscoll

1/15/2008 8:37:00 PM

0

On Jan 15, 2:20 pm, "Erik Lind" <el...@spamcop.net> wrote:
> 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

Erik Lind

1/16/2008 6:46:00 PM

0


"Mike Driscoll" <kyosohma@gmail.com> wrote in message
news:0571a7cd-f7f6-44b4-a4d5-e427de93590a@c4g2000hsg.googlegroups.com...
> On Jan 15, 2:20 pm, "Erik Lind" <el...@spamcop.net> wrote:
>> 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

BTW,

The wxPython group that you mentioned....is that
http://wxforum.sha... ?





Mike Driscoll

1/16/2008 7:07:00 PM

0

On Jan 16, 12:45 pm, "Erik Lind" <el...@spamcop.net> wrote:
> "Mike Driscoll" <kyoso...@gmail.com> wrote in message
>
> news:0571a7cd-f7f6-44b4-a4d5-e427de93590a@c4g2000hsg.googlegroups.com...
>
> > On Jan 15, 2:20 pm, "Erik Lind" <el...@spamcop.net> wrote:
> >> 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
>
> BTW,
>
> The wxPython group that you mentioned....is thathttp://wxforum.sha... ?

I think those are C++ users using the pure C++ wx. I meant the
following:

http://wxpython.org/ma...

Mike