我如何使用此代码通过使用 after 命令直接多次更改我的图像
How Can I use this code to directly change my image more than one time by using after command
我真的不确定我在哪里犯了错误但是我尝试了很多次但没有得到结果,实际上我想在一段时间后在 tkinter window 中更改我的图像而不创建按钮,我从中找到了一些代码在这里并对其进行更改,但我无法在一次后更改图像,图像仅更改一次,在第二张图像静态且不可见后,请帮助找到解决方案。
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()
您需要在 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()
我真的不确定我在哪里犯了错误但是我尝试了很多次但没有得到结果,实际上我想在一段时间后在 tkinter window 中更改我的图像而不创建按钮,我从中找到了一些代码在这里并对其进行更改,但我无法在一次后更改图像,图像仅更改一次,在第二张图像静态且不可见后,请帮助找到解决方案。
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()
您需要在 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()