使用python中的GUI编写一个图片展示界面
时间: 2024-04-29 18:26:03 浏览: 191
您可以使用Python的Tkinter库来创建一个GUI,用于展示图片。以下是示例代码:
``` python
import tkinter as tk
from tkinter import filedialog
from PIL import ImageTk, Image
root = tk.Tk()
root.title("Image Viewer")
# Create a list to hold images
image_list = []
# Function to select images
def select_images():
file_types = (("JPEG files", "*.jpg"), ("PNG files", "*.png"))
file_paths = filedialog.askopenfilenames(filetypes=file_types)
for file_path in file_paths:
# Open image from file path
image = Image.open(file_path)
# Add image to list
image_list.append(image)
# Function to display images
def display_images():
for image in image_list:
# Resize image to fit window
image = image.resize((500, 500), Image.ANTIALIAS)
# Convert image to Tkinter format
img = ImageTk.PhotoImage(image)
# Create label to display image
image_label = tk.Label(root, image=img)
# Display label
image_label.pack()
# Create button to select images
select_button = tk.Button(root, text="Select Images", command=select_images)
select_button.pack()
# Create button to display images
display_button = tk.Button(root, text="Display Images", command=display_images)
display_button.pack()
# Start the GUI
root.mainloop()
```
这段代码创建一个显示窗口,其中有两个按钮:“Select Images”和“Display Images”。点击“Select Images”按钮可选择要显示的一组图片。点击“Display Images”按钮可在窗口中显示所有已选择的图片。您可以根据需要修改此代码以满足您的需求。
阅读全文