Odd behavior in Python/Tkinter?
Lie
Lie.1296 at gmail.com
Sat Dec 22 08:17:47 EST 2007
On Dec 22, 7:35 pm, Fredrik Lundh <fred... at pythonware.com> wrote:
> 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.
Sure I'm listening (well, actually I'm reading), but please spare me
since I can't understand what you meant in the previous posts (I'm
just starting Python, and yesterday was my first introduction to
Tkinter/Tk/Tcl). Everyone has their own newbie moments...
> 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.
That clears things up. I never realized that we can even specify the
name for the Tcl's variable, setting its values is a behavior, but
setting its name is... some kind of incomplete encapsulation (well its
understandable, complete encapsulation is never a Pythonic thing and
proper ways to keep the encapsulation is available [through
StringVar]).
> 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.
After reading your post, I realized the reason why it doesn't
replicate was because I set the variable to an empty string, which
doesn't initialize the textvariable.
More information about the Python-list
mailing list