import tkinter as tk from os import getcwd as pwd import csv def tk_09(): """Using the .csv file you created for the last challenge, create a program that will allow people to add names and ages to the list and create a button that will display the contents of the .csv file by importing it to a list box.""" file_path = f"{pwd()}/tkgui/files/names2.csv" def add_person(): name = txt_in_name.get() age = txt_in_age.get() if age.isdigit(): new_person = [name, age] with open(file_path, '+a') as file: writer = csv.writer(file) writer.writerow(new_person) clear_entries() updt_lbl_file() else: clear_entries() def clear_entries(): txt_in_name.delete(0, 'end') txt_in_age.delete(0, 'end') txt_in_name.focus() def read_csv(): clear_entries() box_out.delete(0, 'end') with open(file_path, 'r') as file: reader = csv.reader(file) contnt = list(reader) for line in contnt: box_out.insert('end',(line[0].ljust(100)+" "+line[1].ljust(10)).rjust(130)) updt_lbl_file() def updt_lbl_file(): path_csv = f"Ruta: {file_path}" lbl_file["state"] = "normal" lbl_file.delete(0, 'end') lbl_file.insert(0, path_csv) lbl_file["state"] = "readonly" window = tk.Tk() window.title("Personas TK") window.geometry("600x330") lbl_name = tk.Label(text="Nombre") lbl_name.place(x=40, y=25, width=100, height=35) lbl_age = tk.Label(text= "Edad") lbl_age.place(x=40, y=60, width=100, height=35) txt_in_name = tk.Entry() txt_in_name["justify"] = "center" txt_in_name.place(x=140, y=25, width=150, height=35) txt_in_name.focus() txt_in_age = tk.Entry() txt_in_age["justify"] = "center" txt_in_age.place(x=140, y=60, width=150, height=35) btn_add = tk.Button(text="Agregar a archivo", command=add_person) btn_add.place(x=300, y=25, width=250, height=35) btn_save = tk.Button(text="Importar archivo", command=read_csv) btn_save.place(x=300, y=60, width=250, height=35) box_out = tk.Listbox() box_out.place(x=40, y=110, width=510, height=160) lbl_file = tk.Entry() lbl_file["state"] = "readonly" lbl_file["justify"] = "center" lbl_file["width"] = 580 lbl_file.place(x=10, y=280, width=580, height=35) window.mainloop()