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

Michael Lange klappnase at web.de
Tue Jun 12 14:00:27 CEST 2012


Hi,

Thus spoketh Wayne Werner <wayne at waynewerner.com> 
unto us on Tue, 12 Jun 2012 06:11:28 -0500 (CDT):

> I'm somewhat certain that it will work just fine, at least it does
> everywhere I've used it.

I agree. Although I am too lazy to dig through the tkinter sources, I am
quite sure that the tkapp object allows True / False as well as 0 / 1 (as
integers) and their respective string counterparts as boolean values. I
did a little experiment with getboolean() :

>>> from Tkinter import *
>>> root = Tk()
>>> getboolean(1)
1
>>> getboolean('1')
True
>>> getboolean(True)
True
>>> getboolean('True')
True
>>> getboolean('true')
True
>>> getboolean(1.0)
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 float
>>> getboolean('abc')
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)
_tkinter.TclError: expected boolean value but got "abc"

So it looks like any string that tk accepts as boolean is legal as well
as 0 / 1 or True / False .

Regards

Michael

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

Leave bigotry in your quarters; there's no room for it on the bridge.
		-- Kirk, "Balance of Terror", stardate 1709.2


More information about the Tkinter-discuss mailing list