python_by_example/tkgui/tk15.py
2023-11-19 02:49:30 -03:00

54 lines
1.8 KiB
Python

from os import getcwd as pwd
import tkinter as tk
def tk_15():
"""Save several images in the same folder as your program and call them
1.gif, 2.gif, 3.gif, etc. Make sure they are all .gif files. Display one
in a window and ask the user to enter a number. It should then use that
number to choose the correct file name and display the correct image."""
path_base = f"{pwd()}/tkgui/imgs"
com_bg = "DarkOrchid2"
window = tk.Tk()
window.geometry("310x420")
window.configure(background=com_bg)
title_icon = tk.PhotoImage(file = f"{path_base}/devfzn_64x64.png")
window.iconphoto(False, title_icon)
photo1 = tk.PhotoImage(file = f"{path_base}/devfzn_250x250.png")
lbl_img = tk.Label(window, image=photo1)
lbl_img["image"] = photo1
lbl_img["bg"] = com_bg
lbl_img.place(x=30,y=160,width=250,height=250)
ent_num = tk.Entry(font="Verdana 20")
ent_num["justify"] = "center"
ent_num.place(x=200, y=100, width=50, height=50)
lbl_msg = tk.Message(text="Ingresa un número (1-4)", font="Verdana 16")
lbl_msg["width"] = 150
lbl_msg["bg"] = com_bg
lbl_msg["justify"] = "center"
lbl_msg.place(x=40, y=95, width=150, height=60)
def clicked():
sel = ent_num.get()
sel = sel.replace(' ', '')
if sel.isdigit() and (0 < int(sel) < 5):
photo = tk.PhotoImage(file = f"{path_base}/img_{sel}.png")
lbl_img.image = photo
else:
ent_num.delete(0, 'end')
photo = tk.PhotoImage(file = f"{path_base}/error.png")
lbl_img.image = photo
lbl_img["image"] = photo
lbl_img.update()
ent_num.focus()
button = tk.Button(text="Click", command=clicked)
button["bg"] = "tomato"
button.place(x=40, y=30, width=220, height=50)
ent_num.focus()
window.mainloop()