[Tutor] Problems passing a parameter in a GUI
James Reynolds
eire1130 at gmail.com
Tue Jan 18 23:19:40 CET 2011
I took another look at what you are trying to do, and if I wanted to count
the number of times the button was pressed in the same way you are trying I
would do something like this instead:
from Tkinter import *
class Application(Frame):
"""GUI App"""
def __init__(self, master):
"""Initialize the frame."""
Frame.__init__(self, master)
self.grid()
self.create_widgets()
self.c = Counter()
> 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):
self.c.a +=1
Calmexob = Calmex(self.c.a)
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 Counter:
a = 0
> class Calmex(object):
def __init__(self,a):
"""Default arg"""
self.a = a
def getmess(self):
mesg="Test" + str(self.a)
return mesg
> #main
root=Tk()
root.title("Test")
root.geometry("400x300")
app=Application(root)
root.mainloop()
I haven't looked at the whole program, just what you have already done.
Really, all you want to do is keep track of how many times "reveal" gets
called, or at least I think that is what you are trying to do, so just add a
simple counter, which I chose to use an object to do.
I then passed the attribute holding this value to instantiate your Calmex
instance and once getmess gets called it should do the right thing.
This might not be what you are after though, at the end of the day.
On Tue, Jan 18, 2011 at 5:02 PM, James Reynolds <eire1130 at gmail.com> wrote:
> You have other issues at play here, but to answer your question from below:
>
> You are calling the method "reveal" prior to creating the global object
> "secret_text"
>
> You can try moving:
>
> x= self.submit_bttn=Button(self, text="Submit", command=self.reveal(x))
>
>
> below self.secret_text = "...."
>
> You will also find that you have not defined something called
> "self.submit_bttn"
>
> and there are more errors beyond that, but that's past the scope of your
> question.
>
> On Tue, Jan 18, 2011 at 4:36 PM, David Holland <davholla2002 at yahoo.co.uk>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.
>>
>>
>>
>>
>>
>> _______________________________________________
>> Tutor maillist - Tutor at python.org
>> To unsubscribe or change subscription options:
>> http://mail.python.org/mailman/listinfo/tutor
>>
>>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20110118/f63e52f8/attachment-0001.html>
More information about the Tutor
mailing list