473,881 Members | 1,516 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Calling button-click events from different classes

3 New Member
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) '.

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 4290
dunderhead
3 New Member
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 Recognized Expert Expert
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
6612
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 written in C++ and it's calling a java application. This application displays a window and pushing a button is calling back the c++-plugin again.
14
3021
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() ' This method shows the form used to get the info If sWhereInt = "" Then DoCmd.OpenForm sFormNameInt, acNormal, , , acFormAdd, _
3
2106
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 presses "Update", the caption changes to "Updating..." and the button disables (to prevent multiple form
4
1777
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 this: <asp:Button ID="Finish" runat="server" OnClick="sendEmail"> However, in this new web site version, when the button is pressed, to scripts are supose to run. So i replaced the button code with this:
7
1565
by: Roger | last post by:
Is this possible?? client side: <script language="jscript"> function hello() { window.alert("HELLO!"); } </script>
5
15825
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 has buttons and it would look cleaner to use a button. I've tried the below in a few variations, including using a href tags inside the onclick. Anyone have a suggestion on what I'm doing wrong here? <input type="button"...
0
1437
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 that formview page they might do a bunch of postbacks, so the back button might not always have the desired affect of taking them back to the calling page. problem #1, a normal back, will only take them to the page from the prior postback, which...
2
2795
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 created the instance of this class and executed, I require the selected item to be sent back to the calling function, and the window should not be destroyed. When the button is pressed I require the control to get back to calling function and process...
3
5281
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 instantaneous, from C# is about 1.5 seconds long. For example, this command powers a device based on an interface switch and returns a message in Ack_Msg that is len long. Besides returns an error code.
4
3595
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 order, Input field and a submit button. The Category name is being fetched from the oracle db along with the corresponding Category order. In the corresponding input field (text box) the user enters a new category order which gets stored in the...
0
9930
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, weíll explore What is ONU, What Is Router, ONU & Routerís main usage, and What is the difference between ONU and Router. Letís take a closer look ! Part I. Meaning of...
0
9776
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
11103
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10403
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7113
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5781
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5978
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4597
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
3225
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.