Odd behavior in Python/Tkinter?

Fredrik Lundh fredrik at pythonware.com
Sat Dec 22 01:42:07 EST 2007


Lie wrote:

>>> Inspect the following code:
 >>>
>>> --- start of code ---
>>> import Tkinter as Tk
>>> from Tkconstants import *
>>> root = Tk.Tk()
>>> e1 = Tk.Entry(root, text = 'Hello World')
>>> e2 = Tk.Entry(root, text = 'Hello World')
 >>
>> the "text" (or "textvariable") option to the Entry widget is the name of
>> the Tcl variable that should hold the result.
>>
>> to get something that's a bit more usable from Python, use a StringVar
>> instance instead.  alternatively, use the "get" method to fetch text
>> from the widget, and the "insert" method to add text to it.

> I realized that 'text' isn't a normally valid arguments for Entry (and
> I'm also aware about the insert, get, etc), but what makes me creep is
> the fact that the Entry's value would mirror each other if they're set
> to the same 'text' value

note that you've set the "textvariable" option, not the "text" option. 
Tk allows you to abbreviate option names.

and since you've set the "textvariable" option for both widgets to the 
same variable, you've asked them both to display the same variable. 
Tkinter just does what you've asked it to.

this is no different from using a Tkinter variable to display the same 
value in a number of radiobutton widgets.

</F>




More information about the Python-list mailing list