This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author terry.reedy
Recipients gpolo, mark, roger.serwy, terry.reedy
Date 2012-06-23.22:06:44
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1340489205.84.0.908569399705.issue15133@psf.upfronthosting.co.za>
In-reply-to
Content
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.
History
Date User Action Args
2012-06-23 22:06:45terry.reedysetrecipients: + terry.reedy, mark, gpolo, roger.serwy
2012-06-23 22:06:45terry.reedysetmessageid: <1340489205.84.0.908569399705.issue15133@psf.upfronthosting.co.za>
2012-06-23 22:06:45terry.reedylinkissue15133 messages
2012-06-23 22:06:44terry.reedycreate