Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

string - Issue with 'StringVar' in Python Program

I am trying to write a VERY simple UI in Python using Tkinter. I have run into a small problem with the StringVar class. The thing is, when I run the python script, I get an error on the line that initializes the StringVar variable. I have written a sample program with this issue that I would like to get working:

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 a feeling that this is an issue with my Python installation, but it may be that I am doing something wrong? I am using python version 2.6.5 on Ubuntu Linux if that makes a difference.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

Just do this:

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...