Python 为什么这段代码会使Tkinter窗口不断自动调整大小/增长?

Python 为什么这段代码会使Tkinter窗口不断自动调整大小/增长?,python,tkinter,callback,python-imaging-library,tkinter-canvas,Python,Tkinter,Callback,Python Imaging Library,Tkinter Canvas,它本应使图像标签随窗口调整大小,但整个窗口(连同图像)都会根据自己的想法调整大小 import tkinter as tk root = tk.Tk() canvas = tk.Canvas(root, height=500, width=500) canvas.pack() from PIL import Image, ImageTk def resize_image(event): new_width = event.width new_height = event.

它本应使图像标签随窗口调整大小,但整个窗口(连同图像)都会根据自己的想法调整大小

import tkinter as tk

root = tk.Tk()

canvas = tk.Canvas(root, height=500, width=500)
canvas.pack()

from PIL import Image, ImageTk

def resize_image(event):
    new_width = event.width
    new_height = event.height
    image = copy_of_image.resize((new_width, new_height))
    photo = ImageTk.PhotoImage(image)
    label.configure(image = photo)
    label.image = photo

image = Image.open("*file path to image*")
copy_of_image = image.copy()
photo = ImageTk.PhotoImage(image)
label = tk.Label(canvas, image=photo)
label.bind('<Configure>', resize_image)
label.pack(fill='both', expand='yes')

root.mainloop()
将tkinter作为tk导入
root=tk.tk()
canvas=tk.canvas(根,高度=500,宽度=500)
canvas.pack()
从PIL导入图像,ImageTk
def resize_图像(事件):
新建宽度=event.width
新高度=事件高度
图像=复制图像。调整大小((新宽度,新高度))
photo=ImageTk.PhotoImage(图像)
label.configure(图像=照片)
label.image=照片
image=image.open(“*文件路径到image*”)
copy\u of_image=image.copy()
photo=ImageTk.PhotoImage(图像)
label=tk.label(画布,图像=照片)
label.bind(“”,调整图像大小)
label.pack(fill='both',expand='yes')
root.mainloop()

每当小部件改变大小时,您都在调用函数。在该函数中,将图像的大小调整为标签的大小。但是,图像周围可能有边框或填充,因此这会导致标签略微增大。由于标签增长,它会触发再次调整图像大小的
事件,这会导致标签更改大小,这会触发再次调整图像大小的
事件,这会导致标签更改大小


解决方案是确保borderwidth和highlightthickness为零,和/或将图像大小调整为比标签小几个像素,这样您就不会强制标签增长。

经过一些测试后,我想我已经找到了您要找的解决方案

我们可以将标签放置在框架中,然后调整框架大小以匹配画布大小,而不是将标签放置在画布上。同时,我们可以调整标签中的图像大小,即使它略大于框架,它也会触发另一次调整大小,直到您直接调整窗口大小

import tkinter as tk
from PIL import Image, ImageTk


root = tk.Tk()


canvas = tk.Canvas(root, height=500, width=500)
canvas.pack(fill='both', expand=True)
frame = tk.Frame(canvas)
canvas.create_window((0, 0), anchor='nw', window=frame, tags='my_frame')


def resize_image(event):
    canvas.itemconfigure("my_frame", width=event.width, height=event.height)
    image = copy_of_image.resize((event.width, event.height ))
    photo = ImageTk.PhotoImage(image)
    label.configure(image=photo)
    label.image = photo


image = Image.open("*file path to image*")
copy_of_image = image.copy()
photo = ImageTk.PhotoImage(image)
label = tk.Label(frame, image=photo)
label.pack(fill='both', expand=True)
canvas.bind('<Configure>', resize_image)

root.mainloop()
将tkinter作为tk导入
从PIL导入图像,ImageTk
root=tk.tk()
canvas=tk.canvas(根,高度=500,宽度=500)
canvas.pack(fill='both',expand=True)
frame=tk.frame(画布)
canvas.create_window((0,0),anchor='nw',window=frame,tags='my_frame')
def resize_图像(事件):
canvas.itemconfigure(“我的框架”,宽度=event.width,高度=event.height)
image=复制图像的大小((event.width,event.height))
photo=ImageTk.PhotoImage(图像)
label.configure(图像=照片)
label.image=照片
image=image.open(“*文件路径到image*”)
copy\u of_image=image.copy()
photo=ImageTk.PhotoImage(图像)
标签=tk.标签(框架,图像=照片)
label.pack(fill='both',expand=True)
canvas.bind(“”,调整图像大小)
root.mainloop()
有关OOP解决方案,请参见以下内容:

import tkinter as tk
from PIL import Image, ImageTk


class App(tk.Tk):
    def __init__(self):
        super().__init__()
        self.canvas = tk.Canvas(self)
        self.frame = tk.Frame(self.canvas)
        self.canvas.pack(fill='both', expand=True)

        image = Image.open('./Images/green.png')
        self.copy_of_image = image.copy()
        photo = ImageTk.PhotoImage(image)
        self.label = tk.Label(self.frame, image=photo)
        self.label.pack(fill='both', expand=True)
        self.label.image = photo

        self.canvas.create_window((0, 0), anchor='nw', window=self.frame, tags='my_frame')
        self.canvas.bind("<Configure>", self.on_canvas_configure)

    def on_canvas_configure(self, event):
        self.canvas.itemconfigure("my_frame", width=event.width, height=event.height)
        image = self.copy_of_image.resize((event.width, event.height))
        photo = ImageTk.PhotoImage(image)
        self.label.configure(image=photo)
        self.label.image = photo


if __name__ == "__main__":
    App().mainloop()
将tkinter作为tk导入
从PIL导入图像,ImageTk
类应用程序(tk.tk):
定义初始化(自):
super()。\uuuu init\uuuuu()
self.canvas=tk.canvas(self)
self.frame=tk.frame(self.canvas)
self.canvas.pack(fill='both',expand=True)
image=image.open('./Images/green.png')
self.copy\u of\u image=image.copy()
photo=ImageTk.PhotoImage(图像)
self.label=tk.label(self.frame,image=photo)
self.label.pack(fill='both',expand=True)
self.label.image=照片
self.canvas.create_window((0,0),anchor='nw',window=self.frame,tags='my_frame')
self.canvas.bind(“,self.on\u canvas\u configure)
画布上的def配置(自身、事件):
self.canvas.itemconfigure(“我的框架”,宽度=事件宽度,高度=事件高度)
image=self.copy\u of_image.resize((event.width,event.height))
photo=ImageTk.PhotoImage(图像)
self.label.configure(image=photo)
self.label.image=照片
如果名称=“\uuuuu main\uuuuuuuu”:
App().mainloop()

您需要使用画布吗?这只需一个标签即可完成。