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