AttributeError:类Tk没有属性'tk'

时间:2015-07-30 22:53:27

标签: python python-2.7 tkinter

嘿伙计们我坚持使用简单的python GUI程序

import Tkinter as tk

window = tk.Tk
text_box = tk.Entry(window)


def save_text():
    str1 = text_box.get()
    fx = open("file1.txt", "w")
    fx.write(str1)
    fx.close()

btn1 = tk.Button(window, text="Save", command="save_text")
text_box.pack()
btn1.pack()
window.mainloop()

我犯了这个错误:

Traceback (most recent call last):
  File "C:/Users/Saket/PycharmProjects/guiform1/firstform.py", line 5, in <module>
    text_box = tk.Entry(window)
  File "C:\Python27\lib\lib-tk\Tkinter.py", line 2385, in __init__
    Widget.__init__(self, master, 'entry', cnf, kw)
  File "C:\Python27\lib\lib-tk\Tkinter.py", line 1965, in __init__
    BaseWidget._setup(self, master, cnf)
  File "C:\Python27\lib\lib-tk\Tkinter.py", line 1943, in _setup
    self.tk = master.tk
AttributeError: class Tk has no attribute 'tk'

任何人都知道我做错了什么请帮助我?

1 个答案:

答案 0 :(得分:2)

您必须致电tk.Tk来创建实例:

window = tk.Tk()
              ^^

然后,您将有一个tk.Tk类型的对象与之交互。