In the wxPython book there is an example of a simple button frame (in the example button.py), where you click on the button and the button label changes to ‘clicked’. If you have two buttons and you want the same result, i.e
. the label to change on whichever button you click, how can you do this without defining two separate OnClick methods for the two buttons?
For example:
self.button = wx.Button(panel, -1, “Hello”, pos=(50,20))
self.Bind(wx.EVT_BUTTON, self.OnClick, self.button)
self.button.SetDefault()
self.button2 = wx.Button(panel, -1, “Hello”, pos=(150,20))
self.Bind(wx.EVT_BUTTON, self.OnClick, self.button2)
self.button2.SetDefault()
def OnClick(self,event):
self.button.SetLabel(“Clicked”)
···
This will only change the first button regardless of which button was the source of the click. How do I pass source information to the OnClick method so that it can change the label of the button that was pressed?
Thanks,
Gabriel
The wx.Event object holds data about the control that posted the event.
The GetEventObject method returns the object, so all you need to do is
change your OnClick method to:
def OnClick(self,event):
event.GetEventObject().SetLabel("Clicked")
As an aside, you appear to be using absolute positioning to lay out your
controls. I'd suggest that, unless you have a good reason to use it, you
take a look at sizers before you try to lay out anything non-trivial.
Simon.
···
On Wed, Jul 05, 2006 at 03:28:16PM +0100, Gabriel Murray wrote:
In the wxPython book there is an example of a simple button frame (in
the example button.py), where you click on the button and the button
label changes to 'clicked'. If you have two buttons and you want the
same result, i.e . the label to change on whichever button you click,
how can you do this without defining two separate OnClick methods for
the two buttons?
For example:
self.button = wx.Button(panel, -1, "Hello", pos=(50,20))
self.Bind(wx.EVT_BUTTON, self.OnClick, self.button)
self.button.SetDefault()
self.button2 = wx.Button(panel, -1, "Hello", pos=(150,20))
self.Bind(wx.EVT_BUTTON, self.OnClick, self.button2)
self.button2.SetDefault()
def OnClick(self,event):
self.button.SetLabel("Clicked")
######
Thanks, that works perfect!
Gabriel
···
On 7/5/06, Simon Clay simon.clay@imperial.ac.uk wrote:
On Wed, Jul 05, 2006 at 03:28:16PM +0100, Gabriel Murray wrote:
In the wxPython book there is an example of a simple button frame (in
the example button.py), where you click on the button and the button
label changes to ‘clicked’. If you have two buttons and you want the
same result, i.e . the label to change on whichever button you click,
how can you do this without defining two separate OnClick methods for
the two buttons?
For example:
self.button = wx.Button(panel, -1, “Hello”, pos=(50,20))
self.Bind(wx.EVT_BUTTON, self.OnClick, self.button)
self.button.SetDefault()
self.button2 = wx.Button(panel, -1, “Hello”, pos=(150,20))
self.Bind(wx.EVT_BUTTON, self.OnClick, self.button2
)
self.button2.SetDefault()
def OnClick(self,event):
self.button.SetLabel(“Clicked”)
The wx.Event object holds data about the control that posted the event.
The GetEventObject method returns the object, so all you need to do is
change your OnClick method to:
def OnClick(self,event):
event.GetEventObject().SetLabel(“Clicked”)
As an aside, you appear to be using absolute positioning to lay out your
controls. I’d suggest that, unless you have a good reason to use it, you
take a look at sizers before you try to lay out anything non-trivial.
Simon.
To unsubscribe, e-mail: wxPython-users-unsubscribe@lists.wxwidgets.org
For additional commands, e-mail:
wxPython-users-help@lists.wxwidgets.org
Hi Gabriel,
Gabriel Murray wrote:
In the wxPython book there is an example of a simple button frame (in the example button.py), where you click on the button and the button label changes to 'clicked'. If you have two buttons and you want the same result, i.e . the label to change on whichever button you click, how can you do this without defining two separate OnClick methods for the two buttons?
For example:
self.button = wx.Button(panel, -1, "Hello", pos=(50,20))
self.Bind(wx.EVT_BUTTON, self.OnClick, self.button)
self.button.SetDefault()
self.button2 = wx.Button(panel, -1, "Hello", pos=(150,20))
self.Bind(wx.EVT_BUTTON, self.OnClick, self.button2)
self.button2.SetDefault()
def OnClick(self,event):
self.button.SetLabel("Clicked")
what about something like this:
def OnClick(self, event):
obj = event.GetEventObject()
text = obj.GetLabel() + ' - Clicked'
obj.SetLabel(text)
This is untested.
Werner
···
######
This will only change the first button regardless of which button was the source of the click. How do I pass source information to the OnClick method so that it can change the label of the button that was pressed?
Thanks,
Gabriel
Thanks again - I wasn’t aware of how to use the event object. Everything’s working now.
Best,
Gabriel
···
On 7/5/06, Werner F. Bruhin < werner.bruhin@free.fr> wrote:
Hi Gabriel,
Gabriel Murray wrote:
In the wxPython book there is an example of a simple button frame (in
the example button.py), where you click on the button and the button
label changes to ‘clicked’. If you have two buttons and you want the
same result, i.e . the label to change on whichever button you click,
how can you do this without defining two separate OnClick methods for
the two buttons?
For example:
self.button = wx.Button(panel, -1, “Hello”, pos=(50,20))
self.Bind(wx.EVT_BUTTON, self.OnClick, self.button)
self.button.SetDefault()
self.button2 = wx.Button(panel, -1, “Hello”, pos=(150,20))
self.Bind(wx.EVT_BUTTON, self.OnClick, self.button2
)
self.button2.SetDefault()
def OnClick(self,event):
self.button.SetLabel(“Clicked”)
what about something like this:
def OnClick(self, event):
obj = event.GetEventObject
()
text = obj.GetLabel() + ’ - Clicked’
obj.SetLabel(text)
This is untested.
Werner
This will only change the first button regardless of which button was
the source of the click. How do I pass source information to the
OnClick method so that it can change the label of the button that was
pressed?
Thanks,
Gabriel
To unsubscribe, e-mail: wxPython-users-unsubscribe@lists.wxwidgets.org
For additional commands, e-mail:
wxPython-users-help@lists.wxwidgets.org