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 4119
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...
|
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...
|
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!
...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
by: antdb |
last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine
In the overall architecture, a new "hyper-convergence" concept was...
|
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.
...
|
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...
|
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...
|
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...
| |