天天看點

Tkinter監聽視窗大小變化事件并自适應調整元件

初始化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))
           

注冊視窗大小變化事件:

# 注冊(綁定)視窗變動事件
        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.
            # 視窗寬高任一值産生變化,則記錄并使展示高清大圖自适應窗體調整。
            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?
                # 重新設定展示的圖檔大小
                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))
           

筆記:

        一般初次啟動視窗最好預設初始化窗體的寬高。如此一來,在後續元件自适應窗體大小的變化就友善多了,按照縮放比例乘除法即可。