2010-08-14 28 views
18

Me gustaría poder cambiar una imagen en una etiqueta de Tkinter, pero no estoy seguro de cómo hacerlo, excepto para reemplazar el widget en sí.¿Cómo actualizar la imagen de un widget Tkinter Label?

Actualmente, puedo mostrar una imagen de este modo:

import Tkinter as tk 
import ImageTk 

root = tk.Tk() 
img = ImageTk.PhotoImage(Image.open(path)) 
panel = tk.Label(root, image = img) 
panel.pack(side = "bottom", fill = "both", expand = "yes") 
root.mainloop() 

Sin embargo, cuando los accesos de los usuarios, dicen que la clave ENTER, me gustaría cambiar la imagen.

import Tkinter as tk 
import ImageTk 

root = tk.Tk() 

img = ImageTk.PhotoImage(Image.open(path)) 
panel = tk.Label(root, image = img) 
panel.pack(side = "bottom", fill = "both", expand = "yes") 

def callback(e): 
    # change image 

root.bind("<Return>", callback) 
root.mainloop() 

¿Esto es posible?

Respuesta

29

El método label.configure funciona en panel.configure(image=img).

Lo que olvidé de hacer fue incluir el panel.image=img, para evitar que la recolección de basura elimine la imagen.

La siguiente es la nueva versión:

import Tkinter as tk 
import ImageTk 


root = tk.Tk() 

img = ImageTk.PhotoImage(Image.open(path)) 
panel = tk.Label(root, image=img) 
panel.pack(side="bottom", fill="both", expand="yes") 

def callback(e): 
    img2 = ImageTk.PhotoImage(Image.open(path2)) 
    panel.configure(image=img2) 
    panel.image = img2 

root.bind("<Return>", callback) 
root.mainloop() 

El código original funciona porque la imagen se almacena en la variable global img.

+0

¿Debe la línea en la devolución de llamada leer 'panel.image = img2'? – 101

+0

@figs parece que tendría sentido. No recuerdo específicamente tener que modificar este uso, pero esto también fue hace más de cuatro años. ¿Podrías probarlo para verificar? – skeggse

+0

Sí, ese probablemente fue el problema. Probado con un código ligeramente diferente, pero con el mismo problema. –

0

Otra opción para hacerlo.

Uso de programación orientada a objetos y con una interfaz interactiva para actualizar la imagen.

from Tkinter import * 
import tkFileDialog 
from tkFileDialog import askdirectory 
from PIL import Image 

class GUI(Frame): 

    def __init__(self, master=None): 
     Frame.__init__(self, master) 
     w,h = 650, 650 
     master.minsize(width=w, height=h) 
     master.maxsize(width=w, height=h) 
     self.pack() 

     self.file = Button(self, text='Browse', command=self.choose) 
     self.choose = Label(self, text="Choose file").pack() 
     self.image = PhotoImage(file='cualitativa.gif') 
     self.label = Label(image=self.image) 


     self.file.pack() 
     self.label.pack() 

    def choose(self): 
     ifile = tkFileDialog.askopenfile(parent=self,mode='rb',title='Choose a file') 
     path = ifile.name 
     self.image2 = PhotoImage(file=path) 
     self.label.configure(image=self.image2) 
     self.label.image=self.image2 


root = Tk() 
app = GUI(master=root) 
app.mainloop() 
root.destroy() 

Reemplace 'cualitativa.jpg' para la imagen predeterminada que desea utilizar.

Cuestiones relacionadas