[Tkinter-discuss] StringVar fails to honor None

Michael Lange klappnase at web.de
Thu Jan 17 11:59:00 CET 2013


Hi Russell,

On Wed, 16 Jan 2013 17:11:52 -0600
Russell Adams <RLAdams at AdamsInfoServ.Com> wrote:

> Found this while reverting a data entry form to database values (ie:
> Undo), all my fields became "None". Set changes None to a string,
> creating with value= doesn't.
> 
> Seems inconsistent, is this intentional or to be expected?

this seems like a feature or a bug in tkapp.globalsetvar().

When we look at Variable.__init_(), we find:

class Variable:
    def __init__(self, master=None, value=None, name=None):
        (...)
        if value is not None:
            self.set(value)

so if value == None, value actually won't be applied.
Now in set() it is simply:

    def set(self, value):
        """Set the variable to VALUE."""
        return self._tk.globalsetvar(self._name, value)

apparently globalsetvar() tries its best to convert anything it gets into
a string.
This is probably useful in most cases (for example when we pas
an integer or a float to set()), but shows odd results sometimes. Setting
the StringVar to boolean True or False e.g. actually sets its value to
"1" or "0" ;)
If I pass an empty dict or list to set(), I get the strings
"{}" or "[]", which seems at least to be consistent with the behavior of
set (None).

I guess this is one of the tkinter oddities we have to live with, at
least I don't see how this should be fixed without breaking existing
code. 

Regards

Michael



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

Violence in reality is quite different from theory.
		-- Spock, "The Cloud Minders", stardate 5818.4


More information about the Tkinter-discuss mailing list