首页 > 解决方案 > Tkinter中resize操作不正确

问题描述

class SomeClass(tk.Frame):

def __init__(self, root):
    tk.Frame.__init__(self, root, bg="#ffffff")

    self.label_image = tk.Label(self, bg ="#ffffff")
    self.label_image.pack(anchor=tk.CENTER, fill=tk.X, padx=20, pady=10)
    self.label_image.bind('<Configure>', self._resize_image)


def _resize_image(self, event):
    print("resize_image")
    f_w, f_h = self.winfo_width(), self.winfo_height() - 50
    i_w, i_h = self.img_copy.size
    width = 1
    height = 1

    if f_w/f_h > i_w/i_h:
        height = f_h
        width = int(i_w * f_h/i_h)
    else:
        width =  f_w
        height = int(i_h * f_w/i_w)

    self.image = self.img_copy.resize((width, height), PIL.Image.ANTIALIAS)
    loading_image = ImageTk.PhotoImage(self.image)
    self.label_image.configure(image=loading_image)
    self.label_image.image = loading_image

macOS。当你拉伸屏幕时,代码可以正常工作,但是如果你全屏制作应用程序并打开图片,它会慢慢按比例增加。在 Ubuntu 上,图片仍然很小,根本没有增加。

在终端会看到: resize_image resize_image resize_image resize_image resize_image...

标签: pythontkinter

解决方案


推荐阅读