import os
from tkinter import filedialog, Tk
from PIL import Image, ImageTk, ImageSequence
import tkinter
from win32com.shell import shell, shellcon
#####################
# 第一个完备的图片浏览器
#####################
def 删除文件(filename):
# 删除文件,经过回收站
shell.SHFileOperation((0, shellcon.FO_DELETE, filename, None,
shellcon.FOF_SILENT | shellcon.FOF_ALLOWUNDO | shellcon.FOF_NOCONFIRMATION, None,
None)) # 删除文件到回收站
testpath = r"C:\Users\Administrator\Desktop\测试文件夹"
class Application():
def __init__(self, root):
self.root = root
self.img = None
self.createLable() # 建立Lable对象
# self.files = self.载入路径文件列表(testpath)
self.files = self.载入路径文件列表()
# [{name:"",path="",tag=False}]
self.index = 0
self.显示图片()
def 显示图片(self):
# 这里开始载入文件
path = self.files[self.index]
self.当前图片 = path
if path[-3:] in {"GIF", "gif"}:
self.显示动图(path)
else:
self.显示单个图片(path)
def 显示单个图片(self, path):
# 单图片
image = Image.open(path)
width, height = self.auto_scale(image)
pic_image = image.resize((width, height), Image.ANTIALIAS)
self.img = ImageTk.PhotoImage(image=pic_image)
# self.img 当前图片的文件对象
# 载入文件结束
self.pic_lable['image'] = self.img
def 显示动图(self, path):
self.帧列表 = list(map(ImageTk.PhotoImage, ImageSequence.Iterator(Image.open(path))))
self.帧位 = 0
self.帧总数 = len(self.帧列表)
self.动图路径 = path
self.显示帧()
def 显示帧(self):
if self.当前图片 != self.动图路径:
return
self.pic_lable['image'] = self.帧列表[self.帧位]
self.帧位 = (self.帧位 + 1) % self.帧总数
self.pic_lable.after(100, self.显示帧)
def auto_scale(self, pic_image):
"""自动缩放"""
width, height = pic_image.size # 获取图片的width height
org_rate = width / height
if width > height * (1080 / 720):
# 宽缩放
rate = 1
while True:
scale_width = width * rate
if scale_width <= 1080:
break
rate -= 0.1
scale_height = scale_width / org_rate
return int(scale_width), int(scale_height)
else:
# 高缩放
rate = 1
while True:
scale_height = height * rate
if scale_height <= 720:
break
rate -= 0.1
scale_width = scale_height * org_rate
return int(scale_width), int(scale_height)
def createLable(self):
self.pic_lable = tkinter.Label(self.root, width=1080, height=720)
self.pic_lable.focus_set()
self.pic_lable['image'] = self.img
self.pic_lable.pack()
def 载入路径文件列表(self, path=None):
if not path:
path = filedialog.askdirectory()
self.root.focus_force() # 返回焦点
pic_list = [''.join([path, '/', img]) for img in os.listdir(path)]
return pic_list
def prev(self):
self.show_file(-1)
def next(self):
self.show_file(1)
def show_file(self, n):
self.index += n
if self.index < 0:
self.index = len(self.files) - 1
if self.index > (len(self.files) - 1):
self.index = 0
self.显示图片()
def 删除当前图片(self):
文件路径 = self.files[self.index]
删除文件(文件路径)
self.files.pop(self.index)
self.show_file(0)
# self.显示图片()
def 滚动处理(event):
if event.delta > 0:
app.prev()
else:
app.next()
def 按键(event):
if event.keycode == 37:
app.prev()
elif event.keycode == 39:
app.next()
elif event.keycode == 46:
# 这是删除键的
app.删除当前图片()
root = Tk()
root.title('图片浏览器')
root.geometry("1080x720")
root['bg'] = '#333333'
root.resizable(False, False)
app = Application(root)
root.bind_all("", 滚动处理)
root.bind_all("", 按键)
# 左右的两个按钮1
btnPrev = tkinter.Button(root, text=" < ", command=app.prev)
btnPrev.place(x=-10, y=360)
# type()function None
# 左右的两个按钮2
btnNext = tkinter.Button(root, text=" > ", command=app.next)
btnNext.place(x=1050, y=360)
# 左右的两个按钮2
delbtn = tkinter.Button(root, text=" 删除图片 ", command=app.删除当前图片)
delbtn.place(relx=1.0, rely=1.0,anchor=tkinter.SE)
root.mainloop()