[Tutor] Simple GUI

Evert Rol evert.rol at gmail.com
Sun Mar 4 13:29:18 CET 2012


> 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())

Here's your problem: you're setting command=self.configure(), but you mean command=self.configure.
In the first way, you are actively calling the configure method. In the second, you're setting the command option to point to the method, but not calling it. In the latter case, an instance of Button can then latter call the configure method by running self.command().
So, when you now create a Button, you instantly run the configure method. Of course, at that point self.Nothing has not been defined yet.

If you look carefully at the traceback, you can see exactly this: you go from line 15 in createWidgets immediately to line 19 in configure, because of the last part of line 15.

In short:
   self.Nothing = Button(self,text='Nothing',activebackground='red',cursor='gumby',command=self.configure)
is what you want.

  Evert


>                 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
> 
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor



More information about the Tutor mailing list