2

I was wondering why tkinter won't let me use its StringVar() function.

Relevant part of my code:

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()

(full program here: http://www.2shared.com/file/nANE4rSD/Blah_Blah_Blah.html)

Error:

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'
4

1 回答 1

3

您必须先初始化 tkinter,然后才能使用像 StringVars 这样的 tkinter 对象。

将线root = tk.Tk()从现在的位置移动到以前 GameName = tk.StringVar()的某个位置,问题将得到解决。

于 2013-01-10T10:31:29.883 回答