Calling button-click events from different classes

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • dunderhead
    New Member
    • Nov 2005
    • 3

    #1

    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.dacl ick1() whilst in the MakePanel2 class produces the error ' TypeError: unbound method daclick1() must be called with MakePanel1 instance as first argument (got nothing instead) '.

    Code:
    #!/usr/bin/python
    
    
    import wx
    import time
    
    
    class MakePanel1(wx.Panel):
    	def __init__(self, Parent, *args, **kwargs):
    		wx.Panel.__init__(self, Parent, *args, **kwargs)
    		
    		self.dalabel = wx.StaticText(self, -1, "   panel 1 label   ")
    		self.dabutton1 = wx.Button(self, label="button 1") 
    		self.dabutton1.Bind(wx.EVT_BUTTON, self.daclick1 )
    		self.bs1 = wx.BoxSizer(wx.HORIZONTAL)
    		self.bs1.Add(self.dabutton1,0)
    		self.bs1.Add(self.dalabel,0)
    		self.SetSizer(self.bs1)
    		
    	def daclick1(self, event):
    		self.dalabel.SetLabel(str(time.time()))
    
    
    class MakePanel2(wx.Panel):
    	def __init__(self, Parent, *args, **kwargs):
    		wx.Panel.__init__(self, Parent, *args, **kwargs)
    
    		self.dabutton2 = wx.Button(self, label="button 2") 
    		self.dabutton2.Bind(wx.EVT_BUTTON, self.daclick2 )
    		self.bs2 = wx.BoxSizer(wx.HORIZONTAL)
    		self.bs2.Add(self.dabutton2,0,wx.ALL,20)
    		self.SetSizer(self.bs2)
    
    	def daclick2(self, event):
    		MakePanel1.daclick1()
    
    class DisFrame(wx.Frame):
    	def __init__(self, *args, **kwargs):
    		wx.Frame.__init__(self, *args, **kwargs)
    
    		self.Panel1 = MakePanel1(self)
    		self.Panel2 = MakePanel2(self)
    
    		bs = wx.BoxSizer(wx.VERTICAL)
    		bs.Add(self.Panel1,1,wx.EXPAND);
    		bs.Add(self.Panel2,1,wx.EXPAND);
    
    		self.SetSizer(bs)
    		self.Fit()
    
    
    if __name__ == '__main__':
    	app = wx.App()
    	daframe = DisFrame(None)
    	daframe.Show()
    	app.MainLoop()
    How do I call the button-click function of dabutton1 whilst in the button-click function of dabutton2 ?

    Any assistance appreciated. Thanks.
  • dunderhead
    New Member
    • Nov 2005
    • 3

    #2
    Well, after experimenting with various function calls I managed to find a solution to my problem.

    I changed:

    Code:
    MakePanel1.daclick1()
    to

    Code:
    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.

    Comment

    • bartonc
      Recognized Expert Expert
      • Sep 2006
      • 6478

      #3
      Here's a neat trick. Def in the frame and use Parent.daclick (which sets the label in its panel1).

      Code:
      #!/usr/bin/python
      
      
      import wx
      import time
      
      
      class MakePanel1(wx.Panel):
      	def __init__(self, Parent, *args, **kwargs):
      		wx.Panel.__init__(self, Parent, *args, **kwargs)
      		
      		self.dalabel = wx.StaticText(self, -1, "   panel 1 label   ")
      		self.dabutton1 = wx.Button(self, label="button 1") 
      		## Use Parent method instead of self
      		self.dabutton1.Bind(wx.EVT_BUTTON, Parent.daclick )
      		self.bs1 = wx.BoxSizer(wx.HORIZONTAL)
      		self.bs1.Add(self.dabutton1,0)
      		self.bs1.Add(self.dalabel,0)
      		self.SetSizer(self.bs1)
      
      
      class MakePanel2(wx.Panel):
      	def __init__(self, Parent, *args, **kwargs):
      		wx.Panel.__init__(self, Parent, *args, **kwargs)
      
      		self.dabutton2 = wx.Button(self, label="button 2") 
      		## Use Parent method instead of self
      		self.dabutton2.Bind(wx.EVT_BUTTON, Parent.daclick )
      		self.bs2 = wx.BoxSizer(wx.HORIZONTAL)
      		self.bs2.Add(self.dabutton2,0,wx.ALL,20)
      		self.SetSizer(self.bs2)
      
      class DisFrame(wx.Frame):
      	def __init__(self, *args, **kwargs):
      		wx.Frame.__init__(self, *args, **kwargs)
      
      		self.Panel1 = MakePanel1(self)
      		self.Panel2 = MakePanel2(self)
      
      		bs = wx.BoxSizer(wx.VERTICAL)
      		bs.Add(self.Panel1,1,wx.EXPAND);
      		bs.Add(self.Panel2,1,wx.EXPAND);
      
      		self.SetSizer(bs)
      		self.Fit()
      		
      	def daclick(self, event):
      		self.Panel1.dalabel.SetLabel(str(time.time()))
      
      
      if __name__ == '__main__':
      	app = wx.App()
      	daframe = DisFrame(None)
      	daframe.Show()
      	app.MainLoop()

      Comment

      Working...