简体   繁体   中英

How Can I use this code to directly change my image more than one time by using after command

I really not sure where i do blunders but I try it many time but not get result, actually I wanted to change my image in tkinter window after sometime without creating a button, I find some code from here and do changes on it but I am not able to change image after once, the image is change only once, after second image is static and not change in visible, please help to find a solution.

val = 2000 #milisecond
val1 = 1000 #milisecond
val2 = 5000 #milisecond

from tkinter import *
from PIL import Image, ImageTk

class myGUI(object):
    def __init__(self):
        self.root = Tk()
        self.canvas = Canvas(width=800, height=800, bg='white')
        self.canvas.pack()

        filename="image1.png"
        image = Image.open(filename)
        self.photo = ImageTk.PhotoImage(image)
        self.img = self.canvas.create_image(250, 250, image=self.photo)


        self.root.after(val, self.change_photo)

        self.root.mainloop()

    def change_photo(self):
        filename = "image2.png"
        image = Image.open(filename)

        self.photo = ImageTk.PhotoImage(image)
        self.canvas.itemconfig(self.img, image=self.photo)

        # From Here I will not achieve my goal

        def __init__(self):

            self.root.after(val1, self.change_photo1)


        def change_photo1(self):
            filename = "image1.png"
            image = Image.open(filename)

            self.photo = ImageTk.PhotoImage(image)
            self.canvas.itemconfig(self.img, image=self.photo)



if __name__ == "__main__":
    gui = myGUI()

You need to call .after() inside change_photo() function. As you want to cycle the images, suggest to use itertools.cycle() as below:

from tkinter import *
from PIL import ImageTk
from itertools import cycle

class myGUI(object):
    def __init__(self):
        self.root = Tk()
        self.canvas = Canvas(width=800, height=800, bg='white')
        self.canvas.pack()

        self.val = 1000

        filenames = ("image1.png", "image2.png")
        self.images = cycle(filenames)
        self.photo = ImageTk.PhotoImage(file=next(self.images))
        self.img = self.canvas.create_image(250, 250, image=self.photo)

        self.root.after(self.val, self.change_photo)
        self.root.mainloop()

    def change_photo(self):
        self.photo = ImageTk.PhotoImage(file=next(self.images))
        self.canvas.itemconfig(self.img, image=self.photo)
        self.root.after(self.val, self.change_photo)


if __name__ == "__main__":
    gui = myGUI()

Example without using cycle :

from tkinter import *
from PIL import ImageTk

class myGUI(object):
    def __init__(self):
        self.root = Tk()
        self.canvas = Canvas(width=800, height=800, bg='orange')
        self.canvas.pack()

        self.val = 1000

        self.images = ("image1.png", "image2.png")
        self.img = self.canvas.create_image(250, 250)

        self.change_photo()
        self.root.mainloop()

    def change_photo(self, index=0):
        if index < len(self.images):
            self.photo = ImageTk.PhotoImage(file=self.images[index])
            self.canvas.itemconfig(self.img, image=self.photo)
            self.root.after(self.val, self.change_photo, index+1)
        else:
            print("No more image")


if __name__ == "__main__":
    gui = myGUI()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM