How can I get imagePath variable to update in real time?

  gif, python, tkinter, variables

I want to change the gif when the button is pressed, and the variable does hold the image path string but it just doesn’t change the image on screen. Any ideas?

import tkinter as tk
from tkinter import *
from PIL import Image, ImageTk, ImageSequence

root = Tk()
root.bind("<Escape>", lambda x: root.destroy())
root.geometry("320x240")
root.configure(background='black')

imagePath = Image.open(r'resourcessplash.gif')

def changeGif():
    global imagePath
    imagePath = Image.open(r'resourcesdna.gif')
    print('updated')
    
    

class displayGif():
    def __init__(self, parent):
        self.parent = parent
        self.canvas = tk.Canvas(parent, width=180, height=180)
        self.canvas = Canvas(root, bd=-2, bg='black', width=180, height=180)
        self.canvas.place(x=100, y=30)
        self.sequence = [ImageTk.PhotoImage(img)
        for img in ImageSequence.Iterator(imagePath)]
        self.image = self.canvas.create_image(90,90, image=self.sequence[0])
        self.animate(1)
    def animate(self, counter):
        self.canvas.itemconfig(self.image, image=self.sequence[counter])
        self.parent.after(80, lambda: self.animate((counter+1) % len(self.sequence)))
        
btn = PhotoImage(file = r"resourcesleft-middle.png")
Button(root, text = '', image = btn, bd=0, highlightthickness=0, bg='black', activebackground='black', command=lambda: changeGif()).place(x=0, y=54)

displayGif(root)    
root.focus_force()
root.mainloop()

Source: Python Questions

LEAVE A COMMENT