472,334 Members | 2,366 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,334 software developers and data experts.

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 4119
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

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

Similar topics

7
by: Klaus Friese | last post by:
Hi, i'm currently working on a plugin for Adobe InDesign and i have some problems with that. I'm not really a c++ guru, maybe somebody here has...
14
by: ericellsworth | last post by:
Hi, I'm trying to use a class to pass variables back and forth from a form opened in dialog mode. I have created a class which invokes a form in...
3
by: Paul | last post by:
Hi I am trying to create an "Update" button for my form. So far I have got it working fine, posting back and updating the record. Great! ...
4
by: Miguel Dias Moura | last post by:
Hi, I just uploaded a web site and i am getting an error. I have a script which sends form values to an email using AspNetEmail. The script...
7
by: Roger | last post by:
Is this possible?? client side: <script language="jscript"> function hello() { window.alert("HELLO!"); } </script>
5
by: Sean M. Loftus | last post by:
I have a need to call an html page into the pane I'm viewing using a button on an ASP page. The page itself is an ASP page called from a website. I...
0
by: jobs | last post by:
I have a page where users select from a bunch of gridviews, setting session variables and then hit an a New or Edit button which redirects them to...
2
by: sumanthsclsdc | last post by:
Hello friends, I have a problem, I implemented a class which uses tkinter and displays the window as required, the class will create a window...
3
by: Beorne | last post by:
I have a propertary library dll (used to drive a device) that I call from my C# code. Calling the functions from C++ is really faster than calling...
4
by: raghuvendra | last post by:
Hi I have a jsp page with 4 columns: namely Category name , Category order, Input field and a submit button. All these are aligned in a row. And...
0
better678
by: better678 | last post by:
Question: Discuss your understanding of the Java platform. Is the statement "Java is interpreted" correct? Answer: Java is an object-oriented...
0
by: CD Tom | last post by:
This only shows up in access runtime. When a user select a report from my report menu when they close the report they get a menu I've called Add-ins...
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge...
0
jalbright99669
by: jalbright99669 | last post by:
Am having a bit of a time with URL Rewrite. I need to incorporate http to https redirect with a reverse proxy. I have the URL Rewrite rules made...
0
by: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was...
0
by: Matthew3360 | last post by:
Hi there. I have been struggling to find out how to use a variable as my location in my header redirect function. Here is my code. ...
2
by: Matthew3360 | last post by:
Hi, I have a python app that i want to be able to get variables from a php page on my webserver. My python app is on my computer. How would I make it...
0
by: AndyPSV | last post by:
HOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and on my computerHOW CAN I CREATE AN AI with an .executable...
0
hi
by: WisdomUfot | last post by:
It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.