python_by_example/tkgui/tk11.py

98 lines
3.4 KiB
Python
Raw Normal View History

2023-11-19 02:48:22 -03:00
from os import getcwd as pwd
import tkinter as tk
from random import randint
def tk_11():
"""Create a new program that will generate two random whole numbers between
10 and 50. It should ask the user to add the numbers together and type in
the answer. If they get the question correct, display a suitable image such
as a tick; if they get the answer wrong, display another suitable image such
as a cross. They should click on a Next button to get another question."""
base_path = f"{pwd()}/tkgui/imgs"
window = tk.Tk()
window.title("Sumas TK")
window.geometry("380x470")
title_icon = tk.PhotoImage(file = f"{base_path}/devfzn_64x64.png")
window.iconphoto(False, title_icon)
def generate():
num = randint(10,50)
txt_box_1["fg"] = "black"
txt_box_1["bg"] = "magenta"
txt_box_1["text"] = f"{num}"
num = randint(10,50)
txt_box_2["fg"] = "black"
txt_box_2["bg"] = "magenta"
txt_box_2["text"] = f"{num}"
txt_num_in.delete(0, 'end')
img_ini = tk.PhotoImage(file = f"{base_path}/ini.png")
box_img.image = img_ini
box_img["image"] = img_ini
box_img.update()
lbl_resp["text"] = ""
txt_num_in.focus()
lbl_info = tk.Label(text="SUMA", font="Verdana 50")
lbl_info.place(x=40, y=25, width=300, height=75)
txt_box_1 = tk.Message(text="", font="Verdana 30")
txt_box_1["fg"] = "red"
txt_box_1["bg"] = "cyan"
txt_box_1["justify"] = "center"
txt_box_1["width"] = 80
txt_box_1.place(x=40, y=160, width=80, height=80)
lbl_plus = tk.Label(text="+", font="Verdana 20")
lbl_plus.place(x=125, y=185, width=20, height=20)
lbl_equal = tk.Label(text="=", font="Verdana 20")
lbl_equal.place(x=235, y=185, width=20, height=20)
txt_box_2 = tk.Message(text="", font="Verdana 30")
txt_box_2["fg"] = "red"
txt_box_2["bg"] = "cyan"
txt_box_2["justify"] = "center"
txt_box_2["width"] = 80
txt_box_2.place(x=150, y=160, width=80, height=80)
def check():
num_1 = txt_box_1["text"]
num_2 = txt_box_2["text"]
resp = txt_num_in.get()
resp = resp.replace(' ', '')
if resp.isdigit() and (int(resp) == int(num_1)+int(num_2)):
lbl_resp["text"] = "¡Correcto!"
img_resp = tk.PhotoImage(file = f"{base_path}/ok.png")
box_img.image = img_resp
else:
lbl_resp["text"] = "¡Incorrecto!"
img_resp = tk.PhotoImage(file = f"{base_path}/error.png")
box_img.image = img_resp
box_img["image"] = img_resp
box_img.update()
btn_launch.focus()
btn_launch = tk.Button(text="Generar", command=generate)
btn_launch.place(x=40, y=110, width=300, height=35)
btn_launch.focus()
txt_num_in = tk.Entry(font="Verdana 30")
txt_num_in["fg"] = "black"
txt_num_in["bg"] = "cyan"
txt_num_in["justify"] = "center"
txt_num_in["width"] = 4
txt_num_in.place(x=265, y=160, width=80, height=80)
btn_check = tk.Button(text="Comprobar", command=check)
btn_check.place(x=40, y=260, width=300, height=35)
lbl_resp = tk.Label(text="", font="Verdana 20")
lbl_resp.place(x=40, y=400, width=300, height=75)
img_ini = tk.PhotoImage(file = f"{base_path}/ini.png")
box_img = tk.Label(window, image=img_ini)
box_img["image"] = img_ini
box_img.place(x=138,y=310,width=100,height=100)
window.mainloop()