如何遍历图像列表并在 Tkinter 中显示它们
How can I Iterate through a list of Images and Display them in Tkinter
所以,我的目标是在 Tkinter 中创建一种幻灯片。我有一个像 Images = ["1.png", "2.png", ...] 这样的图像列表,我希望能够遍历列表并在 Tkinter window,概念简单如下:
- 显示图片 1
- 30 秒延迟
- 显示图片 2
- 30 秒延迟
- 显示图片 3
我已经设法通过按下按钮来迭代图像,但是,我不想必须单击按钮,因为它是为了模仿幻灯片放映,我还尝试循环一个函数,但是 time.sleep() 函数不会以正确的方式延迟,因为 Tkinter 的行为方式。
我主要使用 中的源代码实现了上述目标,如果有人能帮助实现上述目标,我将不胜感激。
我的代码:
from tkinter import *
from PIL import ImageTk, Image
Window = Tk()
Window.geometry("1920x1080")
Window.resizable(0, 0)
Label1 = Label(Window)
Label1.pack()
Images = iter(["1.png", "2.png", "3.png", "4.png", "5.png",
"6.png", "7.png", "8.png", "9.png", "10.png"])
def Next_Image(Val):
try:
Image1 = next(Images)
except StopIteration:
return
Image1 = ImageTk.PhotoImage(Image.open(Image1))
Label1.Image = Image1
Label1["image"] = Image1
Button1 = Button(text = "Next image", command =
lambda:Next_Image(1))
Button1.place(x = 50, y = 50)
Next_Image(1)
Window.mainloop()
我也尝试过使用.after()
,但是,它并没有显示每张图片,它从第一张图片直接跳到最后一张,并带有复合延迟。
for x in range(1, 11):
Window.after(1000, lambda : Next_Image(1))
您需要创建一个函数,从列表中获取图像并显示它,然后使用 after
稍后再次调用自身。您的主程序只需要调用一次,然后它将 运行 直到它 运行 无事可做。
这里是一个工作示例,为了简单起见,它使用了文本字符串,但是如何修改它以使用图像应该是显而易见的。
import tkinter as tk
images = iter(["1.png", "2.png", "3.png", "4.png", "5.png",
"6.png", "7.png", "8.png", "9.png", "10.png"])
def next_image():
try:
image = next(images)
label.configure(text=image)
root.after(1000, next_image)
except StopIteration:
return
root = tk.Tk()
label = tk.Label(root, width = 40, height=4)
label.pack()
next_image()
root.mainloop()
您可以使用.after()
定期切换图片:
from itertools import cycle
...
# use cycle() instead of iter()
Images = cycle([f"{i}.png" for i in range(1, 5)])
...
def next_image():
# use next() to get next image in the cycle list
Label1.image = ImageTk.PhotoImage(file=next(Images))
Label1['image'] = Label1.image
# switch image again after 1 second
Label1.after(1000, next_image)
next_image() # start the loop
Window.mainloop()
成功了,谢谢@acw1668 和@Bryan Oakley。
from tkinter import *
from PIL import ImageTk, Image
Window = Tk()
Window.geometry("1920x1080")
Window.resizable(0, 0)
Label1 = Label(Window)
Label1.pack()
Images = iter(["1.png", "2.png", "3.png", "4.png", "5.png", "6.png",
"7.png", "8.png", "9.png", "10.png"])
def Next_Image(Val):
try:
Image1 = next(Images)
except StopIteration:
return
Image1 = ImageTk.PhotoImage(Image.open("BuyingConfig\" + Image1))
Label1.Image = Image1
Label1["image"] = Image1
Window.after(3000, lambda:Next_Image(1))
Window.after(0, lambda:Next_Image(1))
Window.mainloop()
所以,我的目标是在 Tkinter 中创建一种幻灯片。我有一个像 Images = ["1.png", "2.png", ...] 这样的图像列表,我希望能够遍历列表并在 Tkinter window,概念简单如下:
- 显示图片 1
- 30 秒延迟
- 显示图片 2
- 30 秒延迟
- 显示图片 3
我已经设法通过按下按钮来迭代图像,但是,我不想必须单击按钮,因为它是为了模仿幻灯片放映,我还尝试循环一个函数,但是 time.sleep() 函数不会以正确的方式延迟,因为 Tkinter 的行为方式。
我主要使用
我的代码:
from tkinter import *
from PIL import ImageTk, Image
Window = Tk()
Window.geometry("1920x1080")
Window.resizable(0, 0)
Label1 = Label(Window)
Label1.pack()
Images = iter(["1.png", "2.png", "3.png", "4.png", "5.png",
"6.png", "7.png", "8.png", "9.png", "10.png"])
def Next_Image(Val):
try:
Image1 = next(Images)
except StopIteration:
return
Image1 = ImageTk.PhotoImage(Image.open(Image1))
Label1.Image = Image1
Label1["image"] = Image1
Button1 = Button(text = "Next image", command =
lambda:Next_Image(1))
Button1.place(x = 50, y = 50)
Next_Image(1)
Window.mainloop()
我也尝试过使用.after()
,但是,它并没有显示每张图片,它从第一张图片直接跳到最后一张,并带有复合延迟。
for x in range(1, 11):
Window.after(1000, lambda : Next_Image(1))
您需要创建一个函数,从列表中获取图像并显示它,然后使用 after
稍后再次调用自身。您的主程序只需要调用一次,然后它将 运行 直到它 运行 无事可做。
这里是一个工作示例,为了简单起见,它使用了文本字符串,但是如何修改它以使用图像应该是显而易见的。
import tkinter as tk
images = iter(["1.png", "2.png", "3.png", "4.png", "5.png",
"6.png", "7.png", "8.png", "9.png", "10.png"])
def next_image():
try:
image = next(images)
label.configure(text=image)
root.after(1000, next_image)
except StopIteration:
return
root = tk.Tk()
label = tk.Label(root, width = 40, height=4)
label.pack()
next_image()
root.mainloop()
您可以使用.after()
定期切换图片:
from itertools import cycle
...
# use cycle() instead of iter()
Images = cycle([f"{i}.png" for i in range(1, 5)])
...
def next_image():
# use next() to get next image in the cycle list
Label1.image = ImageTk.PhotoImage(file=next(Images))
Label1['image'] = Label1.image
# switch image again after 1 second
Label1.after(1000, next_image)
next_image() # start the loop
Window.mainloop()
成功了,谢谢@acw1668 和@Bryan Oakley。
from tkinter import *
from PIL import ImageTk, Image
Window = Tk()
Window.geometry("1920x1080")
Window.resizable(0, 0)
Label1 = Label(Window)
Label1.pack()
Images = iter(["1.png", "2.png", "3.png", "4.png", "5.png", "6.png",
"7.png", "8.png", "9.png", "10.png"])
def Next_Image(Val):
try:
Image1 = next(Images)
except StopIteration:
return
Image1 = ImageTk.PhotoImage(Image.open("BuyingConfig\" + Image1))
Label1.Image = Image1
Label1["image"] = Image1
Window.after(3000, lambda:Next_Image(1))
Window.after(0, lambda:Next_Image(1))
Window.mainloop()