python画布轮番显示图片
时间: 2023-11-21 17:56:53 浏览: 119
以下是使用Tkinter库在Python中轮番显示图片的示例代码:
```python
import tkinter as tk
from PIL import Image, ImageTk
import os
class ImagePlayer:
def __init__(self, root, image_folder_path):
self.root = root
self.image_folder_path = image_folder_path
self.image_list = os.listdir(self.image_folder_path)
self.current_image_index = 0
self.image_label = tk.Label(self.root)
self.image_label.pack()
self.show_image()
def show_image(self):
image_path = os.path.join(self.image_folder_path, self.image_list[self.current_image_index])
image = Image.open(image_path)
image = image.resize((400, 400), Image.ANTIALIAS)
photo = ImageTk.PhotoImage(image)
self.image_label.config(image=photo)
self.image_label.image = photo
self.current_image_index = (self.current_image_index + 1) % len(self.image_list)
self.root.after(2000, self.show_image)
root = tk.Tk()
image_folder_path = 'your_image_folder_path'
player = ImagePlayer(root, image_folder_path)
root.mainloop()
```
这个示例代码使用Tkinter库创建了一个窗口,并在窗口中轮番显示指定文件夹中的图片。每隔2秒钟,就会自动切换到下一张图片。你可以将 `your_image_folder_path` 替换为你自己的图片文件夹路径。
阅读全文