Python Forum
[Tkinter] How do I change an image dynamically
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tkinter] How do I change an image dynamically
#1
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...
Reply
#2
(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?

def TestLogic():
    stgImg = PhotoImage(file="Stage1.gif")
    label=ttk.Label(root, image=stgImg)
    label.image = stgImg
    return

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
Reply
#3
(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?
 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...
You might consider using Thread. To unblock the mainloop.
Reply
#4
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()
Reply
#5
#! /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()
I welcome all feedback.
The only dumb question, is one that doesn't get asked.
My Github
How to post code using bbtags


Reply
#6
Images in tkinter are odd. If you don't protect the image from garbage collection you will not see the image change.
Reply
#7
(May-28-2020, 08:50 PM)selmansem Wrote: 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()


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.
Reply
#8
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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  [Tkinter] Tkinter don't change the image DQT 2 1,556 Jul-22-2022, 10:26 AM
Last Post: menator01
  How to dynamically change radiobutton text kenwatts275 2 3,282 Mar-05-2021, 02:25 AM
Last Post: deanhystad
  [PyQt] Python PyQt5 - Change label text dynamically based on user Input ppel123 1 13,655 Mar-20-2020, 07:21 AM
Last Post: deanhystad
  [WxPython] wx.StaticBitmap change image royer14 4 8,274 Jul-02-2018, 02:35 PM
Last Post: royer14

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020