simple Tkinter Question

Eric Brunel eric_brunel at despammed.com
Tue Oct 5 04:12:53 EDT 2004


duikboot wrote:
> Maboroshi wrote:
> 
>> Hi I was curious how I can pass arguments through a Tkinter Entry widget
>>
>> say I have an Entry widget and a Button widget I enter text into the 
>> entry and now I want the button to process the text is there a certain 
>> command I have to use to assign the button to the entry or vise versa
>>
>> anyhelp appreciated
> 
> 
> Take a look at 
> http://www.pythonware.com/library/tkinter/introduction/index.htm
> 
> example
> 
> from Tkinter import *
> 
> def pr_text():
>     text = entry.get()
>     print text
> 
> gui=Tk()
> entry=Entry(gui)
> entry.pack()
> button=Button(gui, text="print", command=pr_text)
> button.pack()
> 
> gui.mainloop()

If *that* is the actual problem, I'd use a Tkinter StringVariable to do the job:

from Tkinter import *
root = Tk()
v = StringVar()
Entry(root, textvariable=v).pack()
def pr_text():
   print v.get()
Button(root, text='Print', command=pr_text).pack()
root.mainloop()

Tkinter variables are a better choice because you often want to know what *was* 
in your entry after it has been destroyed from the display. In such a case, 
entry.get() will raise a TclError, because the entry no more exists. If you use 
a variable, it will still be there after the entry was destroyed, so you can 
still read its value.

HTH
-- 
- Eric Brunel <eric (underscore) brunel (at) despammed (dot) com> -
PragmaDev : Real Time Software Development Tools - http://www.pragmadev.com




More information about the Python-list mailing list