I am making user-friendly data input window and want to give it memory of the last input. I am using .txt file where data should be appended line-by-line and trying to read it to the window with readlines() but I am still getting the exception "list index out of range" when there is more than one line in a document. Here is example on how my code looks like:
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')
How can I fix it?
CodePudding user response:
It seems that you were trying to access the first element of the f.readlines() list, which would only work if the file had at least one line. Since your file could have zero or more lines, you should check the length of the list before trying to access an element. This should solve your problem:
from tkinter import Tk, Label, Entry, StringVar, W
import os
class some_class:
def __init__(self):
self.window = Tk()
self.variable = StringVar()
# Create label and entry widgets
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 the 'save.txt' file exists, read the first line and set it as the initial value of the entry widget
if os.path.isfile('save.txt'):
with open('save.txt', 'r') as f:
self.variable.set(f.readlines()[0].strip())
self.window.mainloop()
# Create an instance of the some_class class and get the value of the variable
incode = some_class()
my_string_variable = str(incode.variable.get())
# Append the value of the variable to the 'save.txt' file
with open('save.txt', 'a') as f:
f.write(my_string_variable '\n')
CodePudding user response:
Ok, I finally got it. If somebody is interested, this is how its done:
# creating a empty textfile
if os.path.exists('save.txt'):
os.utime(1)
else:
listc = ['0', '0', '0', '0']
nfile = open("save.txt", 'w')
for line, i in enumerate(listc):
nfile.write(i '\n')
nfile.close()
class some_class:
window=Tk()
variable=StringVar()
variable2=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))
Label(self.window,text="Here is variable2 place").grid(row=2,column=1,sticky=W)
Entry(self.window,textvariable=self.variable2,justify=RIGHT).grid(row=2,column=2,padx=(0,5))
if os.path.isfile('save.txt'):
with open('save.txt','r') as f:
lines=f.readlines()
self.variable.set(lines[0].replace ("\n",""))
self.variable2.set(lines[1].replace ("\n",""))
self.window.mainloop()
incode=some_class()
my_string_variable=str(incode.variable.get())
my_string_variable2=str(incode.variable2.get())
open('save.txt', 'w').close()
with open('save.txt','a') as f:
f.write(my_string_variable '\n')
f.write(my_string_variable2 '\n')