Quiero mostrar una imagen dentro de un Frame que está en una función que a su vez es llamada por un botón. Cuando presiono el botón me muestra el Frame pero no la imagen. Ya he probado a mostrar el Frame con la imagen en la ventana sin necesidad del botón y funciona. El enunciado me exige mostrar la imagen al presionar el botón.
Aquí el código de ejemplo que hace una función similar al programa donde lo quiero implementar:
EDIT: Actualmente ya puedo generar las imágenes pero solo una vez. Al utilizar el botón de limpiar resultado, lo hace bien pero no me permite generar la etiqueta con la imagen nuevamente.
from tkinter import *
from tkinter import ttk
from tkinter import messagebox
from PIL import ImageTk, Image
class CustomError(Exception):
def __init__(self):
Exception.__init__(self)
def pantallaResultado(v):
global error
def asignarImagen(n):
galery = ["manzana.png", "naranja.png", "banana.png", "pera.png", "mora.png", "berenjena.png"]
return galery[n]
try:
if v>=0 and v<6 and error==0:
pic = PhotoImage(file=asignarImagen(v))
lbl2.configure(image=pic)
lbl2.pack()
error = 1
else:
raise CustomError()
except CustomError:
messagebox.showwarning(title="¡ADVERTENCIA!", message="Debe limpiar la pantalla de resultado antes de mostrar una nueva imagen.")
ventana.mainloop()
def limpiarPantalla():
global error
lbl2.destroy()
error =0
error = 0
ventana=Tk()
#Ventana
ventana.title("Colores")
ventana.geometry("360x480")
#Titulo de frame 1
lbl1 = Label(ventana, text="Presione un botón: ")
lbl1.config(
fg="black",
bg="white",
padx=240,
pady=20,
font=("comic sans", 24)
)
lbl1.pack()
#Frame de botones
f1=Frame(ventana, width=320, height=120)
f1.config(
padx=15,
pady=15,
bd=2,
relief=SOLID
)
f1.pack()
#Botones del frame 1
b1= Button(f1, text="Rojo", fg="white", bg="dark red", command=lambda:pantallaResultado(0))
b1.place(relx=0, rely=0)
b1.config(width=7, height=1)
b2=Button(f1, text="Naranja", fg="black", bg="orange", command=lambda:pantallaResultado(1))
b2.place(relx=0.26, rely=0)
b2.config(width=7, height=1)
b3=Button(f1, text="Amarillo", fg="black", bg="yellow", command=lambda:pantallaResultado(2))
b3.place(relx=0.52, rely=0)
b3.config(width=7, height=1)
b4=Button(f1, text="Verde", fg="white", bg="green", command=lambda:pantallaResultado(3))
b4.place(relx=0.78, rely=0)
b4.config(width=7, height=1)
b5=Button(f1, text="Azul", fg="white", bg="blue", command=lambda:pantallaResultado(4))
b5.place(relx=0.26, rely=0.4)
b5.config(width=7, height=1)
b6=Button(f1, text="Violeta", fg="black", bg="violet", command=lambda:pantallaResultado(5))
b6.place(relx=0.52, rely=0.4)
b6.config(width=7, height=1)
b7=Button(f1, text="Limpiar resultado", command=limpiarPantalla)
b7.place(relx=0.11, rely=0.8)
b8=Button(f1, text="Salir", command=quit)
b8.place(relx=0.52, rely=0.8)
b8.config(width=7, height=1)
#Label para mostrar imagen
lbl2 = Label(ventana)
ventana.mainloop()