显示图片:
等比例根据窗口大小调整图片
同路径切换上一张,下一张,并绑定快捷键
# 图片浏览器、 import os from ttkbootstrap import * from pathlib import Path class viewimg: def __init__(self, imgname): self.root = Window(size=(800, 600)) self.nbt = Button(self.root, text='上一张', command=self.previous) self.nbt.pack(side=LEFT) self.nbt1 = Button(self.root, text='下一张', command=self.back) self.nbt1.pack(side=RIGHT) self.root.bind("<Left>",lambda event:self.previous()) self.root.bind("<Right>",lambda event:self.back()) self.canvas = Canvas(self.root, bg='#312b39', highlightthickness=0) self.canvas.pack(side=LEFT, anchor=CENTER, fill=BOTH, expand=YES) self.imgpath = imgname def run(self): global image, png png = Image.open(self.imgpath) filename = Path(self.imgpath) name = filename.name self.root.title(name) image = ImageTk.PhotoImage(image=self.image_resize(image=png, event=None)) self.image1 = self.canvas.create_image(self.win_size()[0] // 2, self.win_size()[1] // 2, image=image) self.root.bind(sequence='<Configure>', func=self.handler_adaptor(self.image_resize, image=png)) self.root.mainloop() def image_resize(self,event=None, image=None): """ 等比例缩放图片 :param event: bind事件 :param image: Image.open(file_path) or Image.open(io.BytesIO(requests.get(url='https://pic2.zhimg.com/v2-2383d0d9ef77715b3cdd61b405dc8aec_1440w.jpg?source=172ae18b').content)) :return: """ if event is None: screen_width, screen_height = self.win_size() else: screen_width, screen_height = event.width, event.height raw_width, raw_height = image.size[0], image.size[1] max_width, max_height = raw_width, screen_height min_width = max(raw_width, max_width) # 按照比例缩放 min_height = int(raw_height * min_width / raw_width) # 第1次快速调整 while min_height > screen_height: min_height = int(min_height * .9533) # 第2次精确微调 while min_height < screen_height: min_height += 1 # 按照比例缩放 min_width = int(raw_width * min_height / raw_height) # 适应性调整 while min_width > screen_width: min_width -= 1 # 按照比例缩放 min_height = int(raw_height * min_width / raw_width) images = image.resize((min_width, min_height), Image.Resampling.LANCZOS) if event is None: return images else: global png self.canvas.delete(self.image1) png = ImageTk.PhotoImage(images) self.canvas.create_image(screen_width // 2, screen_height // 2, image=png) def win_size(self): """ 获取窗口大小,在没有bind事件时使用 """ self.root.update() width = self.root.winfo_width() height = self.root.winfo_height() return width, height def handler_adaptor(self, fun, **kwds): """事件处理函数的适配器,相当于中介,那个event是从那里来的呢,我也纳闷,这也许就是python的伟大之处吧""" return lambda event, fun=fun, kwds=kwds: fun(event, **kwds) def previous(self): global image, png image,png = None, None global fileindex, fatherpath, files, file_num fileindex -= 1 if fileindex == -1: fileindex = file_num - 1 filepath1 = os.path.join(fatherpath, files[fileindex]) self.imgpath = filepath1 self.run() def back(self): global image, png image, png = None, None global fileindex, fatherpath, files, file_num fileindex += 1 if fileindex == file_num: fileindex = 0 filepath2 = os.path.join(fatherpath, files[fileindex]) self.imgpath = filepath2 self.run() image,png = None, None #指定从哪个图片显示 # filepath = "C:/Windows/Web/Screen/img105.jpg" # # filepath = "C:/Windows/Web/Screen" # fatherpath = os.path.dirname(filepath) # 获取该路径的上一级路径 # filename = os.path.basename(filepath) # 获取该路径下的文件名 # files = os.listdir(fatherpath) # 该路径下的所有文件并生成列表 # print(fatherpath,filename,files) # file_num = len(files) # fileindex = files.index(filename) # 获取当前文件的索引值 # 从该路径下第一张图片显示 fatherpath = "C:/Windows/Web/Screen" files = os.listdir(fatherpath) # 该路径下的所有文件并生成列表 file_num = len(files) filepath = os.path.join(fatherpath, files[0]) fileindex = files.index(files[0]) viewimg(filepath).run()