[Python-bugs-list] [ python-Bugs-730506 ] wdg["textvariable"] and setvar/getvar not compatible

SourceForge.net noreply@sourceforge.net
Tue, 06 May 2003 10:48:05 -0700


Bugs item #730506, was opened at 2003-04-30 16:56
Message generated for change (Comment added) made by reowen
You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=105470&aid=730506&group_id=5470

Category: Tkinter
Group: Python 2.3
Status: Open
Resolution: Fixed
Priority: 5
Submitted By: Russell Owen (reowen)
Assigned to: Nobody/Anonymous (nobody)
Summary: wdg["textvariable"] and setvar/getvar not compatible

Initial Comment:
In Python 2.2.2 and earlier, one could do:

root = Tk()
v = StringVar()
l = Label(root, textvariable=v)
l["textvariable"]

and get back the name of the variable. This could then be used in
l.setvar() and l.getvar() to set or get that variable.

Such code is broken in Python 2.3b1 because l["textvariable"] returns a tcl object, and  setvar/getvar don't recognize it.

It's worth fixing because setvar and getvar seem to be the only way to deal with the thing returned by l["textvariable"].

A fix to consider is to enhance setvar and getvar so that they can deal with tcl objects (as well as with names of variables). This would still break any code that demanded a string back, but the usual reason to obtain the variable was to set or get it with setvar or getvar. So I think it would handle most problems.

A less interesting fix is to have l["textvariable"] return a string. (Of course users can also disable returning objects.)

A broader fix is to have l["textvariable"] return the original Tkinter Variable. That'd be truly wonderful. It would eliminate the need for setvar or getvar in all but the strangest cases (and the existing implementation would do fine in all others). It would certainly break some existing code (users would would have to disable returning objects to be fix that), but what better time to introduce it then when Tkinter is actually returning non-strings sometimes? I know I'm dreaming. I looked at Tkinter.py and it doesn't look like much fun to add (though it could be done by keeping a reference in the Tk object).

-- Russell

----------------------------------------------------------------------

>Comment By: Russell Owen (reowen)
Date: 2003-05-06 10:48

Message:
Logged In: YES 
user_id=431773

Thank you very much for the fix!

I do wish to respond to the comment "Your desired behaviour of having ["textvariable"] return a Variable object would break your own code, as setvar would  again complain about a type error."

str(var) returns the name of the underlying tk variable, whether var is a Tkinter Variable or a tcl object. Thus within setvar and getvar one can simply take str(arg) before using it to solve both the original bug report and also my suggested change.

I plan to submit a patch to Tkinter that implements my suggested behavior and I'd hate for folks to think that it was a radical departure. In fact it is only slightly different than the current new behavior of returning a tcl object and potentially has the same solution for getvar and setvar. One gets an object back either way. The Python 2.3b1 behavior is to return an opaque tcl object; my suggestion is simply to return a non-opaque Tkinter Variable object instead.

----------------------------------------------------------------------

Comment By: Martin v. Löwis (loewis)
Date: 2003-05-06 10:27

Message:
Logged In: YES 
user_id=21627

This patch is likely *not* the cause of this problem. The
comparison only involves a single call to getvar, and that
uses a string argument. This patch didn't affect the result
of getvar at all.

----------------------------------------------------------------------

Comment By: Jack Jansen (jackjansen)
Date: 2003-05-06 04:23

Message:
Logged In: YES 
user_id=45365

Tkinter now doesn't work anymore on MacOSX, at least not with Aqua Tk 
(the only one I tried), and I think this fix is to blame.

Calling Tkinter.Tk() aborts with a
RuntimeError: tcl.h version (8.4) doesn't match libtcl.a version (8.4)

Inspection reveals that _tkinter.TCL_VERSION is the string "8.4", whereas
self.tk.getvar('tcl_version') is the floating point number 
8.4000000000000004.

This specific bug can be fixed by comparing to str(tcl_version), but I'm not 
sure whether this is the right fix, more similar problems could be lurking out 
there.

----------------------------------------------------------------------

Comment By: Martin v. Löwis (loewis)
Date: 2003-04-30 22:50

Message:
Logged In: YES 
user_id=21627

This is fixed in _tkinter.c 1.156, by having getvar/setvar
accept tcl objects.

Your desired behaviour of having ["textvariable"] return a
Variable object would break your own code, as setvar would
again complain about a type error.

----------------------------------------------------------------------

You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=105470&aid=730506&group_id=5470