简体   繁体   English

为什么我的背景图片没有自行调整大小? 我该如何解决这个问题?

[英]Why is my background image not resizing itself? How can I solve this?

I'm trying to make the background image rescale with the window size, but it's not working.我正在尝试使用 window 大小重新缩放背景图像,但它不起作用。 How come?怎么来的? how can I solve this?我该如何解决这个问题?

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()

Your new_bg variable is going out of scope so you need to keep it alive by doing background_label.img = new_bg .您的new_bg变量将超出 scope ,因此您需要通过执行background_label.img = new_bg来保持它的活力。 Also to convert a PIL image to a tkinter image you need to use ImageTk.PhotoImage .此外,要将 PIL 图像转换为 tkinter 图像,您需要使用ImageTk.PhotoImage So change your code to:因此,将您的代码更改为:

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 = ImageTk.PhotoImage(resized_bg) # Changed this
    background_label.img = new_bg           # Changed this
    background_label.config(image=new_bg)

label = Label(window, text='this is a test').pack()
window.bind('<Configure>', resizer) 
window.mainloop()

Also you shouldn't really use both .pack and .place on the same widget.此外,您不应该在同一个小部件上同时使用.pack.place You first did this: background_label.place(...) then this: background_label.pack()你首先这样做: background_label.place(...)然后这个: background_label.pack()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我该如何解决这个问题:我的图像没有出现在屏幕上 - How can i solve this: my image doesn't appear on the screen 为什么我的机器人出现错误? 我该如何解决? - Why is an error occurring in my bot? How can I solve it? 如何显示图像并且我的代码一直在后台运行? - How can I display an image and my code keeps running in the background? 如何在matplotlib中调试背景图片? - How can I debug my background image in matplotlib? 如何将图像固定到Tkinter小部件的背景? - How can I pin an image to the background of my Tkinter widget? 如何在 Tkinter 中为每页添加图像作为背景? - How can I add an image as my background per page in Tkinter? 为什么我的 trie 设置中相同字符的根不同? 以及如何打印 trie 本身? - Why is the root different for the same character in my trie set-up? And how can I print the trie itself? 类型错误,我该如何解决这个错误,在图片中我写了我的具体问题 - Type Error, How can I solve this error, In the image I wrote my specific question 如何在使用PIL调整图像大小后恢复EXIF数据? - How can I restore EXIF data after resizing an image with PIL? 我对 pygame 位图图像有疑问。 图像没有得到我想要的 position 和方向。 我该如何解决? - I have a problem with pygame blit image. Image doesn't get my wanted position and direction. How can I solve?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM