Pages

29 November, 2023

Problem with saving data to a file, the file is left blank most of the time **EDIT i dont really understand, im relatively new to this [duplicate]

I have made a program which is trying to save data to a file however when i try to save it it doesnt go into the file the way i expected it to.
from tkinter import *
from tkinter import messagebox

addPupil = Tk()
addPupil.geometry("275x200")
addPupil.title("Add Pupil")

Label(text = "Add New Pupil", width = 30).grid(columnspan = 3)

x = 1
y = 0

entryboxNames = ["First Name", "Last Name", "School Name", "Guardian Name", "Gender"]

for i in range(5):
Label(text = entryboxNames[i]).grid(row = x, column = y)
ent = Entry()
Entry().grid(row = x, column = y + 1)
x = x + 1

def save():
file = open("details.txt", "a")
file.write(entryboxNames[i] + ":" + ent.get() + " " + "\n\n")
messagebox.showinfo("Saved", "Data saved")
file.close

save = Button(text = "Save", command = save).grid(row = 6, column = 0)



for some reason this code isnt saving data to the file, all i can see is the final term (gender) with nothing after it even if anyone enters something

No comments:

Post a Comment

Thanks