pythonV3V4/第12讲你的颜值有几分一/课堂成果/课后作业-本地图片浏览器.py
sairate 2cd753d3d9 feat(第7讲 字母卡牌): 实现记忆游戏并添加开始界面
- 新增 jiyi.py 文件,实现字母翻牌记忆游戏功能
- 添加 youxijiemian.py 文件,创建游戏开始界面
- 使用 turtle 和 tkinter 模块分别实现游戏和界面
- 支持选择不同难度的游戏模式
2025-06-29 09:06:19 +08:00

43 lines
1.1 KiB
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

'''本地图片浏览器
任务描述:
创建一个简单的图片浏览器。用户可以从计算机中选择一张图片,并在窗口中指定位置显示。
作业要求:
1. 界面包括【选择】按钮和一个图片显示区域300*300
2. 加载本地图片:使用文件对话框让用户选择一张本地图片
3. 图片缩放:选中的图片进行缩放来匹配显示区域
4. 更新显示:选择新图片后,界面应更新所选图片'''
import tkinter as tk
from PIL import Image, ImageTk
from tkinter import filedialog
def load():
file_path = filedialog.askopenfilename()
if file_path:
img = Image.open(file_path)
img = img.resize((300, 300)) # 缩放图片
imgtk = ImageTk.PhotoImage(img)
label.config(image=imgtk)
label.image = imgtk
root = tk.Tk()
root.title("本地图片浏览器")
root.geometry("350x350")
# 显示图片区域
label = tk.Label(root)
label.pack()
# 按钮用于选择图片
button = tk.Button(root, text="选择图片", command=load)
button.pack()
root.mainloop()