我想从用户那里获取输入数据并将其放入文本文件中,但是出现如下错误:
Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Users\dasom\AppData\Local\Programs\Python\Python35-32\Lib\tkinter\__init__.py", line 1549, in __call__
return self.func(*args)
File "C:/Users/dasom/PycharmProjects/Exercise/4.pyw", line 5, in save_data
filed.write("Depot:\n%s\n" % depot.get())
AttributeError: 'NoneType' object has no attribute 'get'`
关于 init .py文件中的第1549行,我查了一下,我不明白这是什么问题。
def __call__(self, *args):
"""Apply first function SUBST to arguments, than FUNC."""
try:
if self.subst:
args = self.subst(*args)
return self.func(*args)
except SystemExit:
raise
except:
self.widget._report_exception()
这是我的整个代码
from tkinter import *
def save_data():
filed = open("deliveries.txt", "a")
filed.write("Depot:\n%s\n" % depot.get())
filed.write("Description :\n%s\n" % description.get())
filed.write("Address :\n%s\n" % address.get("1.0", END))
depot.delete(0, END)
description.delete(0, END)
address.delete("1.0", END)
app = Tk()
app.title('Head-Ex Deliveries')
Label(app, text='Depot:').pack()
depot = Entry(app).pack()
Label(app, text="Description:").pack()
description = Entry(app).pack()
Label(app, text='Address:').pack()
address = Text(app).pack()
Button(app, text='save', command=save_data).pack()
app.mainloop()
实际上,我只输入了教科书的代码。对你的帮助表示感谢。感谢。
答案 0 :(得分:1)
如果教科书有这样的代码,那就是一本很差的教科书。这一行:
depot = Entry(app).pack()
正在做两件事。首先,它创建一个Entry
,然后将其放入应用程序。不幸的是,pack()
方法就地执行并返回None
而不是对原始Entry
小部件的引用。拆分:
depot = Entry(app)
depot.pack()
对于将就地方法的None
返回值分配给您希望指向有用对象的引用的所有类似实例,请执行此操作。