wxpython-how to find id/name of button which is clicked for raising click event

I have created 3 Buttons in a panel they all have same onclick event, now I have to find which button has called onclick event and it’s ID

k=len(lst)

#print k

j=400

for i in range(k):

j=j+20

self.Bind(wx.EVT_BUTTON,self.OnButton2Button,id=j) # k buttons invoking same function

def OnButton2Button(self,event):

##what to code here

Hi, Divya,

···

On Wed, Oct 9, 2013 at 11:30 PM, Divya Patel divyapatel.jbp@gmail.com wrote:

I have created 3 Buttons in a panel they all have same onclick event, now I have to find which button has called onclick event and it’s ID

k=len(lst)

#print k

j=400

for i in range(k):

j=j+20

self.Bind(wx.EVT_BUTTON,self.OnButton2Button,id=j) # k buttons invoking same function

def OnButton2Button(self,event):

##what to code here

Can you declare j as a class variable? I.e. use “self.j” instead.

Thank you.

You received this message because you are subscribed to the Google Groups “wxPython-users” group.

To unsubscribe from this group and stop receiving emails from it, send an email to wxpython-users+unsubscribe@googlegroups.com.

For more options, visit https://groups.google.com/groups/opt_out.

event.GetEventObject().GetId()/GetName()
Should do it.
You are probably not using Phoenix yet, but the documentation is
still very very usefull with 2.9.5 or even 2.8.x, just what out for
“New in version” higher then what you use.
Werner

···

On 10/10/2013 08:30, Divya Patel wrote:

        I have created 3 Buttons in a

panel they all have same onclick event, now I have to find
which button has called onclick event and it�s ID

�k=len(lst)

� � � � #print k

� � � � j=400

� � � � for i in range(k):

� � � � � j=j+20

          � � � � �

self.Bind(wx.EVT_BUTTON,self.OnButton2Button,id=j) # k
buttons invoking same function

def OnButton2Button(self,event):

� � � � �##what to code here

http://wxpython.org/Phoenix/docs/html/Button.html
http://wxpython.org/Phoenix/docs/html/Event.html

Hi,

···

On Thursday, October 10, 2013 1:30:16 AM UTC-5, Divya Patel wrote:

I have created 3 Buttons in a panel they all have same onclick event, now I have to find which button has called onclick event and it’s ID

k=len(lst)

#print k

j=400

for i in range(k):

j=j+20

self.Bind(wx.EVT_BUTTON,self.OnButton2Button,id=j) # k buttons invoking same function

def OnButton2Button(self,event):

##what to code here

I actually wrote an article on this subject some time ago. You can read it here:

Or you can just follow Werner’s advice which is exactly what my article is about.

Mike

I have created 3 Buttons in a panel they all have same onclick event,
now I have to find which button has called onclick event and it’s ID

Werner gave you the correct answer to the question, but I suggest using

the ID may not be the best way to solve your problem. Alternatively, you
can pass an argument on though the callback, so you know what button is
clicked:

http://wiki.wxpython.org/Passing%20Arguments%20to%20Callbacks

I find this approach more elegant than messing around with IDs

k=len(lst)
        #print k
        j=400
        for i in range(k):
          j=j+20
          self.Bind(wx.EVT_BUTTON,self.OnButton2Button,id=j) # k buttons
invoking same function

you've got other issues here as well -- you really want to let wx assign

IDs for you, otherwise you could get ID clashes.

and, side note -- it you want every 20 numbers, you can use something like:

range(400,800,20)

HTH,
  -Chris

···

On Thu, Oct 10, 2013 at 12:44 AM, werner <wbruhin@free.fr> wrote:

On 10/10/2013 08:30, Divya Patel wrote:

--
Christopher Barker, PhD

Python Language Consulting
  - Teaching
  - Scientific Software Development
  - Desktop GUI and Web Development
  - wxPython, numpy, scipy, Cython