469,579 Members | 1,157 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,579 developers. It's quick & easy.

Calling button-click events from different classes

Hello,

I am having a problem with function and class syntax.

I have one class (MakePanel1) that creates a button and label. The button-click event of the button is linked to a function (daclick1) that changes the text of the label. This works well.

I have another class (MakePanel2) that creates a second button. I want this second button to call the button-click function of the first button.

My incorrect call MakePanel1.daclick1() whilst in the MakePanel2 class produces the error ' TypeError: unbound method daclick1() must be called with MakePanel1 instance as first argument (got nothing instead) '.

Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/python
  2.  
  3.  
  4. import wx
  5. import time
  6.  
  7.  
  8. class MakePanel1(wx.Panel):
  9.     def __init__(self, Parent, *args, **kwargs):
  10.         wx.Panel.__init__(self, Parent, *args, **kwargs)
  11.  
  12.         self.dalabel = wx.StaticText(self, -1, "   panel 1 label   ")
  13.         self.dabutton1 = wx.Button(self, label="button 1") 
  14.         self.dabutton1.Bind(wx.EVT_BUTTON, self.daclick1 )
  15.         self.bs1 = wx.BoxSizer(wx.HORIZONTAL)
  16.         self.bs1.Add(self.dabutton1,0)
  17.         self.bs1.Add(self.dalabel,0)
  18.         self.SetSizer(self.bs1)
  19.  
  20.     def daclick1(self, event):
  21.         self.dalabel.SetLabel(str(time.time()))
  22.  
  23.  
  24. class MakePanel2(wx.Panel):
  25.     def __init__(self, Parent, *args, **kwargs):
  26.         wx.Panel.__init__(self, Parent, *args, **kwargs)
  27.  
  28.         self.dabutton2 = wx.Button(self, label="button 2") 
  29.         self.dabutton2.Bind(wx.EVT_BUTTON, self.daclick2 )
  30.         self.bs2 = wx.BoxSizer(wx.HORIZONTAL)
  31.         self.bs2.Add(self.dabutton2,0,wx.ALL,20)
  32.         self.SetSizer(self.bs2)
  33.  
  34.     def daclick2(self, event):
  35.         MakePanel1.daclick1()
  36.  
  37. class DisFrame(wx.Frame):
  38.     def __init__(self, *args, **kwargs):
  39.         wx.Frame.__init__(self, *args, **kwargs)
  40.  
  41.         self.Panel1 = MakePanel1(self)
  42.         self.Panel2 = MakePanel2(self)
  43.  
  44.         bs = wx.BoxSizer(wx.VERTICAL)
  45.         bs.Add(self.Panel1,1,wx.EXPAND);
  46.         bs.Add(self.Panel2,1,wx.EXPAND);
  47.  
  48.         self.SetSizer(bs)
  49.         self.Fit()
  50.  
  51.  
  52. if __name__ == '__main__':
  53.     app = wx.App()
  54.     daframe = DisFrame(None)
  55.     daframe.Show()
  56.     app.MainLoop()
  57.  
How do I call the button-click function of dabutton1 whilst in the button-click function of dabutton2 ?

Any assistance appreciated. Thanks.
Sep 25 '06 #1
2 3934
Well, after experimenting with various function calls I managed to find a solution to my problem.

I changed:

Expand|Select|Wrap|Line Numbers
  1. MakePanel1.daclick1()
to

Expand|Select|Wrap|Line Numbers
  1. MakePanel1.daclick1(daframe.Panel1,None)
... and the program works.

daframe.Panel1 in the above call references a panel created by the MakePanel1 class, and this is the class that contains the function daclick1().

Being a python novice I don't understand why such a call is necessary, or if it imposes any potentially hazardous baggage onto an application.
Sep 25 '06 #2
bartonc
6,596 Expert 4TB
Here's a neat trick. Def in the frame and use Parent.daclick (which sets the label in its panel1).

Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/python
  2.  
  3.  
  4. import wx
  5. import time
  6.  
  7.  
  8. class MakePanel1(wx.Panel):
  9.     def __init__(self, Parent, *args, **kwargs):
  10.         wx.Panel.__init__(self, Parent, *args, **kwargs)
  11.  
  12.         self.dalabel = wx.StaticText(self, -1, "   panel 1 label   ")
  13.         self.dabutton1 = wx.Button(self, label="button 1") 
  14.         ## Use Parent method instead of self
  15.         self.dabutton1.Bind(wx.EVT_BUTTON, Parent.daclick )
  16.         self.bs1 = wx.BoxSizer(wx.HORIZONTAL)
  17.         self.bs1.Add(self.dabutton1,0)
  18.         self.bs1.Add(self.dalabel,0)
  19.         self.SetSizer(self.bs1)
  20.  
  21.  
  22. class MakePanel2(wx.Panel):
  23.     def __init__(self, Parent, *args, **kwargs):
  24.         wx.Panel.__init__(self, Parent, *args, **kwargs)
  25.  
  26.         self.dabutton2 = wx.Button(self, label="button 2") 
  27.         ## Use Parent method instead of self
  28.         self.dabutton2.Bind(wx.EVT_BUTTON, Parent.daclick )
  29.         self.bs2 = wx.BoxSizer(wx.HORIZONTAL)
  30.         self.bs2.Add(self.dabutton2,0,wx.ALL,20)
  31.         self.SetSizer(self.bs2)
  32.  
  33. class DisFrame(wx.Frame):
  34.     def __init__(self, *args, **kwargs):
  35.         wx.Frame.__init__(self, *args, **kwargs)
  36.  
  37.         self.Panel1 = MakePanel1(self)
  38.         self.Panel2 = MakePanel2(self)
  39.  
  40.         bs = wx.BoxSizer(wx.VERTICAL)
  41.         bs.Add(self.Panel1,1,wx.EXPAND);
  42.         bs.Add(self.Panel2,1,wx.EXPAND);
  43.  
  44.         self.SetSizer(bs)
  45.         self.Fit()
  46.  
  47.     def daclick(self, event):
  48.         self.Panel1.dalabel.SetLabel(str(time.time()))
  49.  
  50.  
  51. if __name__ == '__main__':
  52.     app = wx.App()
  53.     daframe = DisFrame(None)
  54.     daframe.Show()
  55.     app.MainLoop()
  56.  
Dec 21 '06 #3

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

7 posts views Thread by Klaus Friese | last post: by
7 posts views Thread by Roger | last post: by
5 posts views Thread by Sean M. Loftus | last post: by
4 posts views Thread by raghuvendra | last post: by
reply views Thread by suresh191 | last post: by
4 posts views Thread by guiromero | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.