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("<MouseWheel>", 滚动处理)
root.bind_all("<Key>", 按键)
# 左右的两个按钮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()

最新文章

  1. Oracle nvchar2和varchar2区别分析
  2. virtual 修饰符 C# .NET
  3. C语言实现泛型编程
  4. linux手动或者自动启动oracle11g的服务 Oracle 自动启动脚本
  5. android 98 MediaPlayer+SurfaceView播放视频
  6. 《Linear Algebra and Its Applications》-chaper3-行列式-克拉默法则
  7. 微信支付:redirect_uri参数错误 的解决办法
  8. const在c和c++中的不同
  9. TxDragon的训练5
  10. Jmeter(二十五)_Xpath关联
  11. Qt Creator的下载和安装
  12. [LeetCode] 115. Distinct Subsequences_ Hard tag: Dynamic Programming
  13. Java中的一个类型转换问题
  14. 记一次vcsa6修复过程
  15. 下载文件的一致性验证之MD5值校验
  16. CF数据结构练习
  17. Core 中 Filter 中相关处理
  18. UEditor 在ie中报console未定义解决方案
  19. [leetcode]Triangle @ Python
  20. nyoj-489-dinic/建图

热门文章

  1. 【一句话】:first-child 伪类解释
  2. zookeeper04---ZAB协议
  3. Mybatis 区别-开发
  4. Hibernate多表关系
  5. JZOJ 3252. 【GDOI三校联考】炸弹
  6. JavaSE 对象与类(一)
  7. Python学习方向
  8. vue element-table滚动条样式修改
  9. WPF ScrollViewer 没有效果
  10. 基于Python的OpenGL 01 之Hello Triangle