Could you file an issue about this on bugs.python.org <http://bugs.python.org/>?

Ronald

—

Twitter / micro.blog: @ronaldoussoren
Blog: https://blog.ronaldoussoren.net/

> On 13 Dec 2020, at 03:16, Ivo Shipkaliev <ivo.shipkal...@gmail.com> wrote:
> 
> Hiya :)
> 
> I'm recently working with tkinter and I noticed that:
> 
> >>> import tkinter as tk
> >>> string_var = tk.StringVar()
> 
> is throwing an AttributeError:
> 
> > Traceback (most recent call last):
> >   . . .
> >   File "...\lib\tkinter\__init__.py", line 505, in __init__
> >     Variable.__init__(self, master, value, name)
> >   File "...\lib\tkinter\__init__.py", line 335, in __init__
> >     self._root = master._root()
> > AttributeError: 'NoneType' object has no attribute '_root'
> 
> ... which is making me question myself:
> -- "Why am I looking for a '_root' attribute on a NoneType object here?", or
> -- "How did I end up with a NoneType, to be searched for a '_root' attribute 
> in the first place?"
> I have done something wrong or I don't know something apparently.
> 
> Following the traceback, looking at "tkinter\__init__.py", lines 333-335 
> leads us to the implementation of StringVar's parent: class Variable:
> 
> 333 >         if not master:
> 334 >             master = _default_root
> 335 >         self._root = master._root()
> 
> Class Variable constructor interface:
> 
> 317 >     def __init__(self, master=None, value=None, name=None):
> 
> Clearly, on line 335, we are relying on the "master" to have a "_root" 
> callable. If "master" was not declared upon class instantiation (line 317), a 
> value can come from _default_root, which is a global module variable. 
> _default_root is holding a Tk instance, if one was instantiated. So, if 
> neither "master" nor _default_root was defined, line 335 will result in an 
> AttributeError.
> 
> Now, would it be a good idea if we keep the user better informed by making 
> the aforementioned lines look something like?:
> 
> 333 >         if not master:
> 334 >             master = _default_root
> 335 >             if not master:
> 336 >                 raise RuntimeError(f"{self.__class__.__name__!r}: 
> 'master' must be defined or a Tk instance must be present!")
> 337 >         self._root = master._root()
> 
> ... and not relying on this inexplicable AttributeError.
> 
> Thank you for your time!
> 
> Regards
> Ivo Shipkaliev
> _______________________________________________
> Python-ideas mailing list -- python-ideas@python.org
> To unsubscribe send an email to python-ideas-le...@python.org
> https://mail.python.org/mailman3/lists/python-ideas.python.org/
> Message archived at 
> https://mail.python.org/archives/list/python-ideas@python.org/message/FSQUFJJQDNSRN4HI7VFXWCNO46YLXQDS/
> Code of Conduct: http://python.org/psf/codeofconduct/

_______________________________________________
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at 
https://mail.python.org/archives/list/python-ideas@python.org/message/LSMSO4VCE5NMFPZGHYFLCOVI25SGCC5A/
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to