Terry J. Reedy <tjre...@udel.edu> added the comment:

Just that, which I used to verify with 2.7.3 and 3.3.0a4 in Win7 and do some 
more experiments:
bv.set/get happily accept *and return* 2, -1, 
bv.set(2.2) 'works'
bv.get() then raises TypeError: must be str, not float

This will annoy you ;-) bv.set('1'); bv.get() returns True, '0'=>False
most other string inputs raise ValueError: invalid literal for getboolean() 
(but see below).

Doc: "There are many useful subclasses of Variable already defined: StringVar, 
IntVar, DoubleVar, and BooleanVar."

Looking into tkinter.__init__, *all* subclasses just inherit Variable.set(self, 
value): return self._tk.globalsetvar(self._name, value). So there is no input 
validation or conversion. I wonder is here should be?

It appears that anything can be set to anything. I was thinking that everything 
was converted to a string, and the error message for 2.2 above suggests that, 
but the (surprising to me) different behavior of 1 and '1' says that is not 
right. So does iv=IntVar(); iv.set(2.2); iv.get() returning 2.

Variable.get(self) returns self._tk.globalgetvar(self._name). 
String/Int/Double/Var call str/int/float as appropriate. BooleanVar calls 
tk.getboolean which is supposed to "Convert true and false to integer values 1 
and 0.". Well, it should not do that and does not do that for tcl false and 
true, as with '0' and '1'. Further checking shows that 'no', 'false', 'yes', 
'true' or any prefix thereof return False or True as desired.

So getboolean was partially changed after the introduction of bool to return 
bool instead int for tcl booleans, but it was not changed to convert ints to 
bool. And if one inputs a Python bool, it somehow gets converted to an int 
instead of being returned as is. I see three possible fixes, but I do not know 
enough of tk/inter usage to choose.

1) add BooleanVar.set to convert Python bool to tcl bool. Other python objects 
could be converted, except that some people might be using python strings that 
become tcl bools (as above) which come back as python bools.

2. change getboolean to not convert python bool (if that is where is happens) 
and do convert int to bool (just 0,1 or all ints?).

3. change BooleanVar.get to convert 0/1 (or all returns?) to False/True.

----------

_______________________________________
Python tracker <rep...@bugs.python.org>
<http://bugs.python.org/issue15133>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to