我想知道为什么tkinter不会让我使用它的StringVar()
函数。
我的代码的相关部分:
import tkinter as tk, os
font = ("Curlz MT", 14, "bold")
font2 = ("Curlz MT", 10, "bold")
GameName = tk.StringVar()
root = tk.Tk()
root.geometry('240x50+0+0')
root.title("Name?")
EyourName = tk.Entry(font=font, width=16, textvariable=GameName)
EyourName.insert(tk.END, "Waiting...")
EyourName.place(x=8,y=8)
Bok = tk.Button(font=font2, width=2, text="Ok", command=Ok)
Bok.place(x=200, y=8)
root.mainloop()
(完整的程序:http://www.2shared.com/file/nANE4rSD/Blah_Blah_Blah.html)
错误:
Traceback (most recent call last):
File "C:\Documents and Settings\SOMENAME\Desktop\David\Python\MonstaWorld\NewGame2.py",
line 34, in <module>
GameName = tk.StringVar()
File "C:\Python32\lib\tkinter\__init__.py", line 243, in __init__
Variable.__init__(self, master, value, name)
File "C:\Python32\lib\tkinter\__init__.py", line 174, in __init__
self._tk = master.tk
AttributeError: 'NoneType' object has no attribute 'tk'
答案 0 :(得分:3)
在使用像StringVars这样的tkinter对象之前,必须先初始化tkinter。
将行root = tk.Tk()
从现在的位置移到之前 GameName = tk.StringVar()
,问题就会解决。