如何使用此代码通过after命令多次直接更改图像

2024-03-28 19:03:28 发布

您现在位置:Python中文网/ 问答频道 /正文

我真的不知道我在哪里犯了错误,但我尝试了很多次,但没有得到结果,实际上我想在tkinter窗口中更改我的图像,但没有创建按钮,我从这里找到一些代码并对其进行更改,但我无法更改一次图像,图像仅更改一次,在第二个图像是静态的且不可见的变化后,请帮助寻找解决方案

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()

1条回答
网友
1楼 · 发布于 2024-03-28 19:03:28

您需要在change_photo()函数内调用.after()。要循环图像,建议使用itertools.cycle(),如下所示:

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()

不使用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()

相关问题 更多 >