[Tutor] Simple GUI
Dave Angel
d at davea.name
Sun Mar 4 13:37:04 CET 2012
On 03/04/2012 06:59 AM, myles broomes wrote:
> Im trying to code a simple GUI but I'm having a bit of a problem. Heres my code: from tkinter import *class Application(Frame):
> def __init__(self,master=None):
> Frame.__init__(self,master)
> self.grid(sticky=N+S+E+W)
> self.createWidgets() def createWidgets(self):
> top=self.winfo_toplevel()
> top.rowconfigure(0,weight=1)
> top.columnconfigure(0,weight=1)
> self.rowconfigure(0,weight=1)
> self.columnconfigure(0,weight=1)
> self.Nothing = Button(self,text='Nothing',activebackground='red',cursor='gumby',command=self.configure())
> self.Nothing.grid(row=0,column=0,sticky=N+S+E+W) def configure(self):
> self.Nothing.configure(text='Hello!')app = Application()
> app.master.title("The Nothing Button")
> app.mainloop() when I run the batch file, i get this error:Traceback (most recent call last):
> File "C:\Python32\gui2.py", line 21, in<module>
> app = Application()
> File "C:\Python32\gui2.py", line 7, in __init__
> self.createWidgets()
> File "C:\Python32\gui2.py", line 15, in createWidgets
> self.Nothing = Button(self,text='Nothing',activebackground='red',cursor='gumby',command=self.configure())
> File "C:\Python32\gui2.py", line 19, in configure
> self.Nothing.configure(text='Hello!')
> AttributeError: 'Application' object has no attribute 'Nothing' What I get from that is that its claiming that my Application class doesnt have an attribute called 'Nothing' but I clearly defined it in the 'createWidgets()' method. Can anyone explain to me exactly what the problem is. Thanks.
>
> Myles Broomes
>
>
Please post in plain-text. Your indentation got messed up in a couple
of places. In this case, i didn't have any trouble deciphering it, but
I've seen cases where the source was totally useless when posted in html.
The problem is simply that you're calling Application.configure() during
the call to create the Button, and you don't assign it to self.Nothing
till the Button object is created. I don't know tkinter well enough
(I've never used it) to know how to fix it, but generally, you want to
fully create a object before you try to call your own methods on it.
One possibility (since I don't know tkinter) is that the command=
argument is supposed to be a function object, which will be called at
some later point. IF that's the case, you want to omit the parentheses
here. .... cursor='gumby', command=self.configure)
HTH,
--
DaveA
More information about the Tutor
mailing list