初始化tkinter窗口:

import tkinter as tk


class MyWindow:
    def __init__(self):
        self.window = tk.Tk()
        # 设置窗口标题
        self.window.title('ImageCape')
        self.window.wm_title('ImageCape')
        # 默认窗口包含标题栏
        self.window.overrideredirect(False)
        # 初始化窗口大小并自适应屏幕居中
        self.window.geometry(str(S_WIDTH) + 'x' + str(S_HEIGHT) + '+'
                             + str((self.window.winfo_screenwidth() - S_WIDTH) // 2) + '+'
                             + str((self.window.winfo_screenheight() - S_HEIGHT) // 2 - 18))
        # 第一次加载,因为监听窗口大小调整事件,初始化时会调用绑定'<Configure>'的函数/方法
        self.first_load = True

注册窗口大小变化事件:

        # 注册(绑定)窗口变动事件
        self.window.bind('<Configure>', self.window_resize)

实践证明,'<Configure>'事件会在被绑定的self.window_resize()方法中多次反馈窗口大小信息,包含重复的。所以需要进行去重监听数据适应变化。

window_resize():

import tkinter as tk


class MyWindow:
    # ...
    def window_resize(self, event=None):
        if event is not None:
            # listen events of window resizing.
            # 窗口宽高任一值产生变化,则记录并使展示高清大图自适应窗体调整。
            # 1)
            if self.window_width != self.window.winfo_width() or self.window_height != self.window.winfo_height():
                if self.window_width != self.window.winfo_width():
                    self.window_width = self.window.winfo_width()
                if self.window_height != self.window.winfo_height():
                    self.window_height = self.window.winfo_height()
                # What happens here?
                if self.first_load:
                    self.first_load = False
                else:
                    # 重新设置展示的图片大小
                    self.load_image(self.image_pos)
            '''
            # 2)
            # 其中self.can为self.photo_canvas的容器,self.photo_canvas是显示图像的画布,两者都是Canvas类型。Amaz!22/2/20 Sun.)
            self.win_resized = self.can.winfo_width() != self.win_width or self.can.winfo_height() != self.win_height
            if self.win_resized:
                # Canvas has been resized.
                self.win_width = self.can.winfo_width()
                self.win_height = self.can.winfo_height()
                if self.first_load:
                    self.first_load = False
                else:
                    self.photo_canvas.config(width=self.win_width)
                    self.load_image(self.image_pos)
            '''

load_image():

    def load_image(self, position=0):
        if len(self.caches) > 0 and len(self.caches) > position >= 0:
            try:
                image = resize_image(self.cache_paths[position], screen_width=self.window.winfo_width(),
                               screen_height=self.window.winfo_height())
                photo = ImageTk.PhotoImage(image)
                # 假设这里是使用Label组件显示高清图片
                self.label.config(image=photo)
                self.label.image = photo
                # print(self.caches[position])
            except FileNotFoundError:
                self.reload_caches()
        else:
            photo = None
            self.label.config(image=photo)
            self.label.image = photo

resize_image():

"""
@author: MR.N
@created: 2021-08-22 Sun. 21:54

"""
from PIL import Image, ImageTk

# 初始化窗口宽高
S_WIDTH = 876
S_HEIGHT = 720
# 初始化左边高清大图宽高
I_WIDTH = 710
I_HEIGHT = 682
# 初始化右边缩略图宽高
SUB_WIDTH = 166
SUB_HEIGHT = 166


def resize_image(path, scale=-1, screen_width=0, screen_height=0):
    image = Image.open(path)
    if scale == -1:
        # 高清大图原始宽高
        raw_width, raw_height = image.size[0], image.size[1]
        # 减去右边缩略图宽的最大宽度,减除工具栏(假设有且高40)的最大高度
        max_width, max_height = max(I_WIDTH, screen_width-SUB_WIDTH), max(I_HEIGHT, screen_height-40)
        min_height = min(max_height, raw_height)
        # 按比例缩放高清大图
        min_width = int(raw_width * min_height / raw_height)
        # 如果大图超出窗体显示区域,进行第二次(或多次)缩放
        if min_width > max_width:
            min_width = min(max_width, raw_width)
            min_height = int(raw_height * min_width / raw_width)
    return image.resize((min_width, min_height))

笔记:

        一般初次启动窗口最好预设初始化窗体的宽高。如此一来,在后续组件自适应窗体大小的变化就方便多了,按照缩放比例乘除法即可。

Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐