如何更改图像并放入标签内?

时间:2019-10-06 14:34:35

标签: python tkinter

view = Tk()
view.title("Title")
view.geometry('600x600')
view.attributes("-fullscreen",True)

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.config(image = photo)
    label.image = photo 
image = Image.open('add.png')
copy_of_image = image.copy()
photo = ImageTk.PhotoImage(image)
label = ttk.Label(view, image = photo)
label.bind('<Configure>', resize_image)
label.pack(fill=BOTH, expand = YES)
command="pwd"

def changePicture():
    print("I change picture")
    image2 = Image.open('add.png')
    copy_of_image = image2.copy()
    photo = ImageTk.PhotoImage(image2)
    label = ttk.Label(view, image = photo)
    label.image=image2
    label.pack()

它只能站立并且不能更改图片。我还有其他代码可用于“ add.png”图像。只是窗口静止不动,没有变化。谢谢您的帮助。

1 个答案:

答案 0 :(得分:0)

更改行:

label.pack(fill=BOTH, expand = YES)

label.pack()