[Tkinter-discuss] Is it safe to use True/False for 1/0 in tkinter?

Michael Lange klappnase at web.de
Wed Jun 13 11:13:09 CEST 2012


Hi,

Thus spoketh Mark Summerfield <list at qtrac.plus.com> 
unto us on Wed, 13 Jun 2012 07:36:04 +0100:

> > >>> getboolean(0.0)
> > Traceback (most recent call last):
> >   File "<stdin>", line 1, in <module>
> >   File "/usr/lib/python3.1/tkinter/__init__.py", line 328, in
> > getboolean return _default_root.tk.getboolean(s)
> > TypeError: must be string, not float
> > >>> getboolean(0L)
> > Traceback (most recent call last):
> >   File "<stdin>", line 1, in <module>
> >   File "/usr/lib/python2.6/lib-tk/Tkinter.py", line 336, in getboolean
> >     return _default_root.tk.getboolean(s)
> > TypeError: getboolean() argument 1 must be string, not long
> > 
> > so it actually looks like Tkinter behavior is not fully coherent here.
> 
> Shouldn't this count as a bug then?

OTOH, if you look at

>>> getboolean.__doc__
'Convert true and false to integer values 1 and 0.'
>>> Misc.getboolean.__doc__
'Return a boolean value for Tcl boolean values true and false given as
parameter.'
>>> getboolean('true')
True
>>>

We see that getboolean does what it promises (left aside that the default
boolean return values in Python have changed from 0 / 1 to True / False by
now). According to http://wiki.tcl.tk/16235 valid boolean values in Tcl
are also yes, no, on, off, 0 or 1. In fact all of these seem to work,
even case insensitive:

>>> getboolean('on')
True
>>> getboolean('1')
True
>>> getboolean('yes')
True
>>> getboolean('yEs')
True

That the special values 0 / 1 (integers) and True / False are also
accepted, seems to be a special undocumented extra feature, probably
added so we can pass directly the return value of some Tkinter function
to getboolean() without having to convert it into string first (as you
can see it is done in the function definitions below).

So maybe it is simply that my example function wasn't chosen wisely.

The inconsistency seems to arise when getboolean() is used behind the
scenes and behaves differently tha Tk, as in e.g.

    def wm_overrideredirect(self, boolean=None):
        """Instruct the window manager to ignore this widget
        if BOOLEAN is given with 1. Return the current value if None
        is given."""
        return self._getboolean(self.tk.call(
            'wm', 'overrideredirect', self._w, boolean))

Now if we do 

>>> root.overrideredirect(0.0)

the float is passed to and obviously accepted by Tk, although (according
to the wiki at least) it does not seem to be "correct" use of booleans in
Tcl.

Let's try another Tk method that wants a boolean:

>>> root.tk_strictMotif(0.0)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python2.6/lib-tk/Tkinter.py", line 374, in tk_strictMotif
    'set', 'tk_strictMotif', boolean))
TypeError: getboolean() argument 1 must be string, not float
>>> 

And when we look at what goes on behind the scenes:

    def tk_strictMotif(self, boolean=None):
        """Set Tcl internal variable, whether the look and feel
        should adhere to Motif.

        A parameter of 1 means adhere to Motif (e.g. no color
        change if mouse passes over slider).
        Returns the set value."""
        return self.tk.getboolean(self.tk.call(
            'set', 'tk_strictMotif', boolean))

we see that it's Tk itself which is not consistent, because
tk_strictMotif apparently returns the float where wm overrideredirect
does not.

Now, is this a bug?
Hard to tell, I would not say so, because as long as we stick with the
documented usage everything works as expected and I think we cannot
seriously rely on undocumented features to work coherently in any case.

However, coming back to the original question, as far as the 0/1 vs.
True/False issue is affected, I guess that simply the Tkinter docs could
use an update, as True/False have been the officially preferred bool
values since 2.4 (or so) and are apparently supported by Tkinter.
Probably there was just no one who found the time to do this (?).

Regards

Michael


.-.. .. ...- .   .-.. --- -. --.   .- -. -..   .--. .-. --- ... .--. . .-.

The only solution is ... a balance of power.  We arm our side with exactly
that much more.  A balance of power -- the trickiest, most difficult,
dirtiest game of them all.  But the only one that preserves both sides.
		-- Kirk, "A Private Little War", stardate 4211.8


More information about the Tkinter-discuss mailing list