Odd behavior in Python/Tkinter?

Fredrik Lundh fredrik at pythonware.com
Sat Dec 22 07:35:58 EST 2007


Lie wrote:

> But an expression (e.g. string) is NOT a variable. 

in this case, it is.  I don't know if it's worth spending more time on 
this, since you're not listening, but let's make one more attempt.

for the Entry widget, the "textvariable" argument, if given, identifies 
an *internal* Tkinter variable (managed by the embedded Tcl inter- 
preter, not Python).  changes to this variable will be reflected in the 
widget, and changes to the widget will be reflected in the variable.

the *usual* way to create such a variable is to use StringVar, and leave 
it to Tkinter to come up with an internal variable name, but you can 
refer to any Tcl variable; if it doesn't exist, it's created.

in your example, you told both widgets to use the same internal 
variable.  you can do the same thing with a StringVar:

     var = Tk.StringVar()

     e1 = Tk.Entry(root, textvariable = var)
     e2 = Tk.Entry(root, textvariable = var)

doing this has the advantage that you can access the internal variable 
via the StringVar object (held in the Python variable named "var"), but 
at the Tkinter level, it's exactly the same thing.  changes to the 
variable will be reflected in both widgets, and changes to *either* 
widget will be reflected in the variable, and therefore also in the 
other widget.

 > On the other hand, the oddness multiplied since the value replication
 > doesn't happen if I set the textvariable to a variable.

sure does, if you use the same internal variable for both widgets.

</F>




More information about the Python-list mailing list