python_by_example/tkgui/tk06.py
2023-11-17 13:26:09 -03:00

46 lines
1.4 KiB
Python

import tkinter as tk
def tk_06():
"""Create a window that will ask the user to enter a number in a text box.
When they click on a button it will use the code variable.isdigit() to
check to see if it is a whole number. If it is a whole number, add it to
a list box, otherwise clear the entry box. Add another button that will
clear the list."""
def add_num():
name = txt_in_box.get()
if name.isdigit():
lst_nums.insert('end', name)
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()
window = tk.Tk()
window.title("Lista de números TK")
window.geometry("390x350")
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=150, height=35)
btn_reset = tk.Button(text="Vaciar", command=reset)
btn_reset.place(x=190, y=75, width=150, height=35)
lst_nums = tk.Listbox()
lst_nums["justify"] = "center"
lst_nums.place(x=40, y=120, width=300, height=200)
window.mainloop()