Problem with 'StringVar' in Python program

I am trying to write a VERY simple user interface in Python using Tkinter. I ran into a little problem with the StringVar class. The thing is, when I run the python script, I get an error message in the line initializing the StringVar variable. I wrote an example program with this problem that I would like to get:

 from Tkinter import * var = StringVar() var.set('test'); 

When I run it through python, I see this error:

 $ python test.py Traceback (most recent call last): File "test.py", line 3, in <module> var = StringVar() File "/usr/lib/python2.6/lib-tk/Tkinter.py", line 254, in __init__ Variable.__init__(self, master, value, name) File "/usr/lib/python2.6/lib-tk/Tkinter.py", line 185, in __init__ self._tk = master.tk AttributeError: 'NoneType' object has no attribute 'tk' Exception AttributeError: "StringVar instance has no attribute '_tk'" in <bound method StringVar.__del__ of <Tkinter.StringVar instance at 0xb73cc80c>> ignored 

I have the feeling that this is a problem with my Python installation, but maybe I am doing something wrong? I am using python version 2.6.5 on Ubuntu Linux, if that matters.

+6
source share
2 answers

I think you might need to explicitly call Tk () before calling StringVar.

Just do the following:

 from Tkinter import * Tk() # Add this var = StringVar() var.set('test'); 
+10
source

I have never done anything with Tkinter itself, but here it looks like this StringVar class inherits from the Variable base class, as you can see in the trace with the Variable.__init__() call. An exception was made with the expression "self.tk = master.tk". The following error message indicates that this β€œmain” parameter is NoneType and therefore does not have such a tk attribute. Looking at the Tkinter documentation for StringVar here: http://epydoc.sourceforge.net/stdlib/Tkinter.StringVar-class.html

the main parameter is set to None. It seems that the wizard should be presented as a widget that may contain this StringVar (i.e. does it make sense to have a StringVar not associated with widgets?). I have to say that you definitely need to associate the StringVar object with the widgets so that it has the "tk" attribute.

+1
source

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


All Articles