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) '. - #!/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.
2 4265
Well, after experimenting with various function calls I managed to find a solution to my problem.
I changed:
to - 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.
Here's a neat trick. Def in the frame and use Parent.daclick (which sets the label in its panel1). - #!/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()
-
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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()
'...
|
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...
|
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...
|
by: Roger |
last post by:
Is this possible??
client side:
<script language="jscript">
function hello() {
window.alert("HELLO!");
}
</script>
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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,...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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...
|
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...
|
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
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
| |