首页 > 解决方案 > 更改窗口大小时如何更快地加载背景图像?

问题描述

import tkinter as tk
from tkinter import ttk
from PIL import ImageTk, Image

class Application(tk.Frame):
    '''Application root window'''
    def __init__(self, parent, *args, **kwargs):
        super().__init__(parent, *args, **kwargs)
        self.parent = parent
        self.pack(expand=1)
        root.title("Library")
        root.minsize(width=400, height=400)
        root.geometry("600x600")

        self.image=Image.open('lib4.jpg')
        self.img_copy = self.image.copy()
        self.background_image = ImageTk.PhotoImage(self.image)
        self.background = tk.Label(self, image=self.background_image)
        self.background.pack(fill=tk.BOTH, expand=tk.YES)
        self.background.bind('<Configure>', self._resize_image)

    def _resize_image(self, event):
        new_width = event.width
        new_height = event.height
        self.image = self.img_copy.resize((new_width, new_height))
        self.background_image = ImageTk.PhotoImage(self.image)
        self.background.configure(image=self.background_image)


if __name__ == "__main__":
    root = tk.Tk()
    app = Application(parent=root)
    app.mainloop()

我正在尝试根据窗口大小调整背景图像的大小,但是当我更改窗口大小时,它会逐渐调整。我怎样才能使它快速?提前致谢。

标签: pythontkinter

解决方案


推荐阅读