[Tutor] Problems passing a parameter in a GUI
David Holland
davholla2002 at yahoo.co.uk
Wed Jan 19 22:57:00 CET 2011
Dave,
Sorry I did not include the traceback but now James Reynolds (thank you very much) has provided a solution I don't need the info any more.
Thank you both for your help and time.
Traceback (most recent call last):
File "/home/david/Documents/learnJava2010/v6c.py", line 46, in <module>
app=Application(root)
File "/home/david/Documents/learnJava2010/v6c.py", line 9, in __init__
self.create_widgets(x)
File "/home/david/Documents/learnJava2010/v6c.py", line 15, in create_widgets
x= self.submit_bttn=Button(self, text="Submit", command=self.reveal(x))
File "/home/david/Documents/learnJava2010/v6c.py", line 24, in reveal
self.secret_text.delete(0.0, END)
AttributeError: Application instance has no attribute 'secret_text'
--- On Tue, 18/1/11, Dave Angel <davea at ieee.org> wrote:
From: Dave Angel <davea at ieee.org>
Subject: Re: [Tutor] Problems passing a parameter in a GUI
To: "David Holland" <davholla2002 at yahoo.co.uk>
Cc: tutor at python.org
Date: Tuesday, 18 January, 2011, 22:39
On 01/-10/-28163 02:59 PM, David Holland wrote:
> Hi All,
> I hope this makes sense
>
> I am trying to write a GUI where you click on a button and each time
> you click on the button it shows in the entry text how many times you
> have clicked. However when I changed an earlier version of the code to
> pass a parameter it errors.
>
> This works :-
>
>
>
> from Tkinter import *
>
> class Application(Frame):
>
> """GUI App"""
>
> def __init__(self,
master):
>
> """Initialize the frame."""
>
> Frame.__init__(self, master)
>
> self.grid()
>
> self.create_widgets()
>
>
>
> def create_widgets(self):
>
> self.inst_lbl=Label(self, text="Click on the button to get instructions")
>
> self.inst_lbl.grid(row=0, column=0, columnspan=2, sticky=W)
>
> self.pw_lbl=Label(self, text="Password")
>
> self.submit_bttn=Button(self, text="Submit", command=self.reveal)
>
> self.submit_bttn.grid(row=1, column=1, columnspan=2, sticky=W)
>
>
self.secret_text=Text(self, width=35, height=5, wrap=WORD)
>
> self.secret_text.grid(row=2, column=1, columnspan=2, sticky=W)
>
> self.helpinfo()
>
>
>
> def reveal(self):
>
> Calmexob = Calmex()
>
> results = Calmexob.getmess()
>
> self.secret_text.delete(0.0, END)
>
> self.secret_text.insert(0.0,results)
>
>
>
> def helpinfo(self):
>
> self.secret_text.insert(0.0,"Start")
>
>
>
> class Calmex(object):
>
>
>
> def __init__(self):
>
>
"""Default arg"""
>
>
>
>
>
> def getmess(self):
>
> mesg="Test"
>
> return mesg
>
>
>
> #main
>
> root=Tk()
>
> root.title("Test")
>
> root.geometry("400x300")
>
> app=Application(root)
>
> root.mainloop()
>
>
>
>
>
> But when I changed it to use a number I get an error message.
>
> Here is the code
>
> from Tkinter import *
>
> class Application(Frame):
>
> """GUI App"""
>
> def __init__(self, master):
>
> """Initialize the frame."""
>
> Frame.__init__(self,
master)
>
> self.grid()
>
> x=0
>
> self.create_widgets(x)
>
>
>
> def create_widgets(self,x):
>
> self.inst_lbl=Label(self, text="Click on the button to get instructions")
>
> self.inst_lbl.grid(row=0, column=0, columnspan=2, sticky=W)
>
> self.pw_lbl=Label(self, text="Password")
>
> x= self.submit_bttn=Button(self, text="Submit", command=self.reveal(x))
>
> self.submit_bttn.grid(row=1, column=1, columnspan=2, sticky=W)
>
> self.secret_text=Text(self, width=35, height=5,
wrap=WORD)
>
> self.secret_text.grid(row=2, column=1, columnspan=2, sticky=W)
>
> self.helpinfo()
>
>
>
> def reveal(self, x):
>
> Calmexob = Calmex(x)
>
> results = Calmexob.getmess(x)
>
> self.secret_text.delete(0.0, END)
>
> self.secret_text.insert(0.0,results)
>
> x=x+1
>
> return x
>
>
>
> def helpinfo(self):
>
> self.secret_text.insert(0.0,"Start")
>
>
>
> class Calmex(object):
>
>
>
>
def __init__(self,x):
>
> """Default arg"""
>
>
>
>
>
> def getmess(self,x):
>
> mesg="Test"+str(x)
>
> return mesg
>
>
>
> #main
>
> root=Tk()
>
> root.title("Test")
>
> root.geometry("400x300")
>
> app=Application(root)
>
> root.mainloop()
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
> The error message is
>
> AttributeError: Application instance has no attribute 'secret_text'
>
>
>
> The only change is that I have declared x and changed the function reveal to take and pass x as an
arguement.
>
>
>
> Thanks in advance. I hope this makes sense.
>
>
>
>
>
>
>
>
>
I don't have time to try to understand all the tk stuff. And it sure
would have been nice if you showed the whole error message, including
the traceback. You didn't even tell us what line it was on.
But your problem is that you have an instance of Application without the
secret_text attribute, but you're trying to use it. You should get in
the habit of initializing all attributes in __init__(). Anyway, it
looks like the only places that could get the error are inside the
methods reveal() and selfinfo(). If either gets called before
create_widget() finishes, you could get this error.
The stack trace would show just what combination of method calls is
making this
happen.
DaveA
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20110119/596e73ed/attachment.html>
More information about the Tutor
mailing list