Tkinter中调整大小的错误操作

时间:2018-11-26 10:15:48

标签: python tkinter

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

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

在终端上将看到: resize_image resize_image resize_image resize_image resize_image ...

0 个答案:

没有答案