Getting tkinter StringVar () error on initialization

(Python version: 3.1.1)

I have a strange problem with StringVar in tkinter. Trying to constantly update the Message widget in the project, I continued to receive an error message when I tried to create a variable. I jumped into the python interactive shell to investigate, and here is what I got:

 >>> StringVar <class 'tkinter.StringVar'> >>> StringVar() Traceback (most recent call last): File "<stdin>", line 1, in <module> File "C:\Python31\lib\tkinter\__init__.py", line 243, in __init__ Variable.__init__(self, master, value, name) File "C:\Python31\lib\tkinter\__init__.py", line 174, in __init__ self._tk = master.tk AttributeError: 'NoneType' object has no attribute 'tk' >>> 

Any ideas? Each example of tkinter usage that I saw shows the variable initialization, while nothing is sent to the constructor, so I'm at a loss if I missed something ...

+6
source share
1 answer

StringVar requires a wizard:

 >>> StringVar(Tk()) <Tkinter.StringVar instance at 0x0000000004435208> >>> 

or more often:

 >>> root = Tk() >>> StringVar() <Tkinter.StringVar instance at 0x0000000004435508> 

When you create an instance of Tk, a new interpreter is created. Prior to this, nothing works:

 >>> from Tkinter import * >>> StringVar() Traceback (most recent call last): File "<input>", line 1, in <module> File "C:\Python26\lib\lib-tk\Tkinter.py", line 251, in __init__ Variable.__init__(self, master, value, name) File "C:\Python26\lib\lib-tk\Tkinter.py", line 182, in __init__ self._tk = master.tk AttributeError: 'NoneType' object has no attribute 'tk' >>> root = Tk() >>> StringVar() <Tkinter.StringVar instance at 0x00000000044C4408> 

The problem with the examples you found is that, probably, in the literature they show only partial fragments that should be inside the class or in a longer program, so import and other code are not explicitly specified.

+13
source

Source: https://habr.com/ru/post/905341/


All Articles