473,382 Members | 1,387 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 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 4265
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 an idea how to solve this. The plugin is...
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 its show method, like so: Public Sub Show() '...
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! However, I now want to make it so that when a user...
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 was working when i was calling the script like...
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 know a link would work but the rest of the page...
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 a formview in edit or insert mode. While in...
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 with listbox and inserts some items into it, I...
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 them in C+ +. From C++ the call is almost...
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 Each Category Name has its corresponding Category...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?

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.