simple Tkinter Question

duikboot adijkstra at baanders NOSPAM consultancy.nl
Tue Oct 5 04:32:24 EDT 2004


Eric Brunel wrote:
> 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

You're absolutely right. It's better indeed.

-- 
http://www.baandersconsultancy.nl | http://www.nosonis.com



More information about the Python-list mailing list