[Tutor] help with .get in Tkinter

Michael Lange klappnase at freenet.de
Sun Feb 20 17:11:05 CET 2005


On Sun, 20 Feb 2005 17:12:54 +0200
Mark Kels <mark.kels at gmail.com> wrote:

> Hi all.
> First, here is the code I have a problem with (I got same problem in
> my project) :
> from Tkinter import *
> def go():
>     e.get()
>     print e
> 
> main=Tk()
> e=Entry(main)
> e.pack()
> b=Button(main,text='OK',command=go()).pack()
> main.mainloop()
> 
> For some reason the function is called before I click the button, and
> I get .10037088 before I have done a thing.
> How do I do it right ???
> -- 

Hi Mark,

First problem:

you need to assign the command for the button without parenthesis:

b = Button(main, text='OK', command=go)
b.pack()

I split the line you used into two lines, because pack() returns None , so you don't have a reference
to the button once you created it. Of course you can do it the way you did if you don't need to reference
the button anymore, however there's not much use in assigning a new variable to it, just write:

Button(main,text='OK',command=go()).pack()

Second problem:

your go() function does just what you told it to: it prints the "window name" (or however this is called) of
the entry widget. You surely meant something like this:

def go():
    contents = e.get()
    print contents

or simply:

def go():
    print e.get()

I hope this helps

Michael



More information about the Tutor mailing list