[Tkinter] How do I change an image dynamically - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: GUI (https://python-forum.io/forum-10.html) +--- Thread: [Tkinter] How do I change an image dynamically (/thread-7807.html) |
How do I change an image dynamically - LeeMadeux - Jan-25-2018 I am trying to dynamically change a displayed picture to another picture on a GUI. I have tried various suggested ways seen on websites and none have worked this far. What am I doing wrong? from tkinter import * from tkinter import ttk def TestLogic(): stgImg = PhotoImage(file="Stage1.gif") label=ttk.Label(root, image=stgImg) label.image = stgImg return root = Tk() root.geometry('1010x740+200+200') stgImg = PhotoImage(file="Stage0.gif") label=ttk.Label(root, image=stgImg) label.place(x=400, y=400) testBtn=ttk.Button(root, text="TEST", command=TestLogic) testBtn.place(x=400, y=200) root.mainloop()The first image is displayed just fine. When I press the button, nothing happens. Thanks... RE: How do I change an image dynamically - Barrowman - Jan-25-2018 (Jan-25-2018, 08:49 PM)LeeMadeux Wrote: I am trying to dynamically change a displayed picture to another picture on a GUI. Perhaps this little change might help def TestLogic(): stgImg = PhotoImage(file="Stage1.gif") label=ttk.Label(root, image=stgImg) label.image = stgImg root.update_idletasks() return RE: How do I change an image dynamically - haha001 - Feb-01-2018 (Jan-25-2018, 08:49 PM)LeeMadeux Wrote: I am trying to dynamically change a displayed picture to another picture on a GUI. I have tried various suggested ways seen on websites and none have worked this far. What am I doing wrong?You might consider using Thread. To unblock the mainloop.from tkinter import * from tkinter import ttk def TestLogic(): stgImg = PhotoImage(file="Stage1.gif") label=ttk.Label(root, image=stgImg) label.image = stgImg return root = Tk() root.geometry('1010x740+200+200') stgImg = PhotoImage(file="Stage0.gif") label=ttk.Label(root, image=stgImg) label.place(x=400, y=400) testBtn=ttk.Button(root, text="TEST", command=TestLogic) testBtn.place(x=400, y=200) root.mainloop()The first image is displayed just fine. When I press the button, nothing happens. Thanks... RE: How do I change an image dynamically - selmansem - May-28-2020 Try this... from tkinter import * from tkinter import ttk def TestLogic(): stgImg = PhotoImage(file="Stage1.gif") label.configure(image=stgImg) label.image = stgImg root = Tk() root.geometry('1010x740+200+200') stgImg = PhotoImage(file="Stage0.gif") label=ttk.Label(root, image=stgImg) label.place(x=400, y=400) testBtn=ttk.Button(root, text="TEST", command=TestLogic) testBtn.place(x=400, y=200) root.mainloop() RE: How do I change an image dynamically - menator01 - May-28-2020 #! /usr/bin/env python3 from tkinter import * from tkinter import ttk class Picture: def __init__(self, parent): self.parent = parent img = PhotoImage(file='img1.png') self.label = ttk.Label(self.parent) self.label['image'] = img img.image = img self.label.pack() btn = Button(self.parent, command=self.update, text='Test').pack(side='bottom', pady=50) def update(self): img = PhotoImage(file='img2.png') self.label['image'] = img img.image = img def main(): root = Tk() root.geometry('400x400+50+50') Picture(root) root.mainloop() main() RE: How do I change an image dynamically - deanhystad - May-28-2020 Images in tkinter are odd. If you don't protect the image from garbage collection you will not see the image change. RE: How do I change an image dynamically - moorthypnt - Jun-04-2020 (May-28-2020, 08:50 PM)selmansem Wrote: Try this... It is working. But what is the logic behind "label.image = stgImg " this command. Only this command makes it work. It is completely new addition when compared with text update. RE: How do I change an image dynamically - deanhystad - Jun-04-2020 That command tells sets the image reference count to 1. Python cannot delete the image and reuse the memory. This is what I mean by "Images in tkinter are odd". I would think that setting an image to be used in a button or a label should save that image from garbage collection. I could see if it is just stamping something on a canvas, but python treats all images this way. You need a non-local variable to keep hold of the image handle, preventing the image reference counter from going to zero, for the image to stick around. |