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'
任何人都知道我做错了什么请帮助我?
答案 0 :(得分:2)
您必须致电tk.Tk
来创建实例:
window = tk.Tk()
^^
然后,您将有一个tk.Tk
类型的对象与之交互。