[Tutor] Simple GUI

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

[Tutor] Simple GUI

myles broomes
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


_______________________________________________
Tutor maillist  -  [hidden email]
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor
Reply | Threaded
Open this post in threaded view
|

Re: [Tutor] Simple GUI

Evert Rol
> 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  -  [hidden email]
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor

_______________________________________________
Tutor maillist  -  [hidden email]
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor
Reply | Threaded
Open this post in threaded view
|

Re: [Tutor] Simple GUI

Dave Angel-3
In reply to this post by myles broomes
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

_______________________________________________
Tutor maillist  -  [hidden email]
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor