【发布时间】:2022-12-15 10:42:29
【问题描述】:
我正在制作用户友好的数据输入窗口,并希望为其提供最后一次输入的记忆。我正在使用 .txt 文件,其中数据应逐行附加并尝试使用 readlines() 将其读取到窗口,但当有多于一行时我仍然收到异常“列表索引超出范围”一个文件。这是我的代码的示例:
class some_class:
window=Tk()
variable=StringVar()
def __init__(self):
Label(self.window,text="Here is variable place").grid(row=1,column=1,sticky=W)
Entry(self.window,textvariable=self.variable,justify=RIGHT).grid(row=1,column=2,padx=(0,5))
if os.path.isfile('save.txt'):
with open('save.txt','r') as f:
self.variable.set(f.readlines()[0])
self.window.mainloop()
incode=some_class()
my_string_variable=str(incode.variable.get())
with open('save.txt','a') as f:
f.write(my_string_variable+'\n')
我该如何解决?
【问题讨论】: