【发布时间】:2021-04-06 20:31:41
【问题描述】:
我正在尝试使背景图像随窗口大小重新缩放,但它不起作用。怎么来的?我该如何解决?
from tkinter import *
from PIL import Image, ImageTk
window = Tk()
window.geometry("1300x700")
window.title('Monopoly')
background_image = PhotoImage(file="board.png")
background_label = Label(window, image=background_image)
background_label.place(x=0, y=0, relwidth=1, relheight=1)
background_label.pack()
def resizer(event):
global bg1, resized_bg, new_bg
bg1 = Image.open('board.png')
resized_bg = bg1.resize((event.width, event.height))
new_bg = PhotoImage(resized_bg)
background_label.config(image=new_bg)
label = Label(window, text='this is a test').pack()
window.bind('<Configure>', resizer)
window.mainloop()
【问题讨论】:
-
在
resizer函数末尾添加background_label.img = new_bg。还将new_bg = PhotoImage(resized_bg)更改为new_bg = ImageTk.PhotoImage(resized_bg)
标签: python user-interface tkinter