[Tutor] Problems passing a parameter in a GUI
Dave Angel
davea at ieee.org
Tue Jan 18 23:39:04 CET 2011
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
More information about the Tutor
mailing list