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

51 lines
1.8 KiB
Python

import tkinter as tk
def tk_03():
"""Create a program that will ask the user to enter a number in a box. When
they click on a button it will add that number to a total and display it
in another box. This can be repeated as many times as they want and keep
adding to the total. There should be another button that resets the total
back to 0 and empties the original text box, ready for them to start again."""
def click():
if txt_in_box.get() != '':
num = int(txt_in_box.get())
if txt_out_box["text"] == "0":
txt_out_box["text"] = f"{num}"
else:
res = int(txt_out_box["text"]) + num
txt_out_box["text"] = f"{res}"
txt_in_box.delete(0, 'end')
txt_in_box.delete(0, 'end')
txt_in_box.focus()
def reset():
txt_out_box["text"] = "0"
txt_in_box.delete(0, 'end')
txt_in_box.focus()
window = tk.Tk()
window.title("Suma TK")
window.geometry("390x230")
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()
txt_out_box = tk.Message(text="0")
txt_out_box["fg"] = "black"
txt_out_box["bg"] = "magenta"
txt_out_box["justify"] = "center"
txt_out_box["width"] = 250
txt_out_box["font"] = "TkHeadingFont"
txt_out_box.place(x=40, y=115, width=300, height=50)
btn_acept = tk.Button(text="AGREGAR AL TOTAL", command=click)
btn_acept.place(x=40, y=75, width=300, height=35)
btn_reset = tk.Button(text="BORRAR TOTAL", command=reset)
btn_reset.place(x=40, y=170, width=300, height=35)
window.mainloop()