python_by_example/tkgui/tk01.py

47 lines
1.4 KiB
Python
Raw Normal View History

2023-11-17 13:26:09 -03:00
import tkinter as tk
def tk_01():
"""Create a window that will ask the user to enter their name. When they
click on a btn_acept it should display the message 'Hello' and their
name and change the background colour and font colour of the message
box."""
def click():
name = txt_in_box.get()
if name == "":
reset()
else:
txt_out_box["fg"] = "black"
txt_out_box["bg"] = "magenta"
txt_out_box["text"] = f"Hola {name}"
txt_in_box.delete(0, 'end')
txt_in_box.focus()
def reset():
txt_out_box["fg"] = "red"
txt_out_box["bg"] = "cyan"
txt_out_box["text"] = ""
window = tk.Tk()
window.title("Saludo TK")
window.geometry("390x200")
lbl_info = tk.Label(text="Ingresa tu nombre: ")
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="")
txt_out_box["fg"] = "red"
txt_out_box["bg"] = "cyan"
txt_out_box["justify"] = "center"
txt_out_box["width"] = 250
txt_out_box["font"] = "TkHeadingFont"
txt_out_box.place(x=40, y=120, width=300, height=50)
btn_acept = tk.Button(text="ACEPTAR", command=click)
btn_acept.place(x=40, y=75, width=300, height=35)
window.mainloop()