How to update the image of a Tkinter Label widget? How to update the image of a Tkinter Label widget? tkinter tkinter

How to update the image of a Tkinter Label widget?


The method label.configure does work in panel.configure(image=img).

What I forgot to do was include the panel.image=img, to prevent garbage collection from deleting the image.

The following is the new version:

import Tkinter as tkimport ImageTkroot = 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 = img2root.bind("<Return>", callback)root.mainloop()

The original code works because the image is stored in the global variable img.


Another option to do it.

Using object-oriented programming and with an interactive interface to update the image.

from Tkinter import *import tkFileDialogfrom tkFileDialog import askdirectoryfrom PIL import  Imageclass 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.image2root = Tk()app = GUI(master=root)app.mainloop()root.destroy()

Replace 'cualitativa.jpg' for the default image you want to use.


Another solution that might be of help.

In my case, I had two tk.Tk() windows. When using ImageTk.PhotoImage(), the object defaults to setting its tk window to being the first one created. A simple fix to this is to pass the tk window that you want as such ImageTk.PhotoImage(img, master=your_window)

import tkinter as tk from PIL import ImageTk, Imageif __name__ == '__main__':     main_window = tk.Tk()     second_window = tk.Tk()          main_canvas = Canvas(second_window)     main_canvas.pack()          filename = 'test.png'     img = Image.open(filename)     img = img.resize((300, 100), Image.ANTIALIAS)     logo = ImageTk.PhotoImage(img, master=second_window)     logo_label = Label(master=main_canvas, image=logo)     logo_label.image = logo     logo_label.pack()     main_window.mainloop()