Posts: 3
Threads: 2
Joined: Jan 2018
Jan-25-2018, 08:49 PM
(This post was last modified: Jan-25-2018, 08:54 PM by LeeMadeux.)
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...
Posts: 163
Threads: 13
Joined: Oct 2016
(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
Posts: 4
Threads: 1
Joined: Feb 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? 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.
Posts: 1
Threads: 0
Joined: May 2020
May-28-2020, 08:50 PM
(This post was last modified: May-28-2020, 08:50 PM by selmansem.)
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()
Posts: 1,145
Threads: 114
Joined: Sep 2019
#! /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()
Posts: 6,812
Threads: 20
Joined: Feb 2020
Images in tkinter are odd. If you don't protect the image from garbage collection you will not see the image change.
Posts: 1
Threads: 0
Joined: Jun 2020
(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.
Posts: 6,812
Threads: 20
Joined: Feb 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.
|