python_by_example/tkgui/sols_124-132/sol_07.py

52 lines
1.2 KiB
Python
Raw Normal View History

2023-11-17 13:26:09 -03:00
from tkinter import *
import csv
def add_number():
num = num_box.get()
if num.isdigit():
num_list.insert('end', num)
num_box.delete(0, 'end')
num_box.focus()
else:
num_box.delete(0, 'end')
num_box.focus()
def clear_list():
num_list.delete(0, 'end')
num_box.focus()
def save_list():
with open('/tmp/nums.csv', '+a') as file:
tmp_list = num_list.get(0, 'end')
item = 0
for _ in tmp_list:
newrecord = tmp_list[item]+'\n'
file.write(str(newrecord))
item += 1
file.close()
window = Tk()
window.title("Lista de Números")
window.geometry("400x200")
label1 = Label(text="Ingresa un nro.")
label1.place(x=20, y=20, width=100, height=25)
num_box = Entry()
num_box.place(x=120, y=20, width=100, height=25)
num_box.focus()
button1 = Button(text="Agregar", command=add_number)
button1.place(x=250, y=20, width=100, height=25)
num_list = Listbox()
num_list.place(x=120, y=50, width=100, height=100)
button2 = Button(text="Limpiar", command=clear_list)
button2.place(x=250, y=50, width=100, height=25)
button3 = Button(text="Guardar", command=save_list)
button3.place(x=250, y=80, width=100, height=25)
window.mainloop()