69 lines
2.2 KiB
Python
69 lines
2.2 KiB
Python
import tkinter as tk
|
|
from os import getcwd as pwd
|
|
import csv
|
|
|
|
def tk_07():
|
|
"""Alter program 129 to add a third button that will save the list to a .csv
|
|
file. The code 'tmp_list = num_list.get(0,"end")' can be used to save the
|
|
contents of a list box as a tuple called tmp_list."""
|
|
file_path = f"{pwd()}/tkgui/files/nums.csv"
|
|
def add_num():
|
|
num = txt_in_box.get()
|
|
if num.isdigit():
|
|
lst_nums.insert('end', num)
|
|
txt_in_box.delete(0, 'end')
|
|
txt_in_box.focus()
|
|
else:
|
|
txt_in_box.delete(0, 'end')
|
|
txt_in_box.focus()
|
|
|
|
def reset():
|
|
txt_in_box.delete(0, 'end')
|
|
lst_nums.delete(0, 'end')
|
|
txt_in_box.focus()
|
|
|
|
def save_csv():
|
|
tmp_lst = lst_nums.get(0, 'end')
|
|
with open(file_path, 'a+') as file:
|
|
item = 0
|
|
for _ in tmp_lst:
|
|
new_num = tmp_lst[item]+'\n'
|
|
file.write(str(new_num))
|
|
item += 1
|
|
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("Lista de números TK")
|
|
window.geometry("600x240")
|
|
|
|
lbl_info = tk.Label(text="Ingresa un número: ")
|
|
lbl_info.place(x=40, y=25, width=150, height=35)
|
|
|
|
txt_in_box = tk.Entry()
|
|
txt_in_box["justify"] = "center"
|
|
txt_in_box.place(x=180, y=25, width=150, height=35)
|
|
txt_in_box.focus()
|
|
|
|
btn_add = tk.Button(text="Agregar", command=add_num)
|
|
btn_add.place(x=40, y=75, width=300, height=50)
|
|
btn_reset = tk.Button(text="Vaciar", command=reset)
|
|
btn_reset.place(x=40, y=130, width=140, height=50)
|
|
btn_save = tk.Button(text="Guardar", command=save_csv)
|
|
btn_save.place(x=200, y=130, width=140, height=50)
|
|
|
|
lst_nums = tk.Listbox()
|
|
lst_nums["justify"] = "center"
|
|
lst_nums.place(x=350, y=25, width=220, height=160)
|
|
|
|
lbl_file = tk.Entry()
|
|
lbl_file["state"] = "readonly"
|
|
lbl_file["justify"] = "center"
|
|
lbl_file["width"] = 580
|
|
lbl_file.place(x=10, y=190, width=580, height=35)
|
|
|
|
window.mainloop()
|