提问人:António Rebelo 提问时间:11/17/2023 最后编辑:António Rebelo 更新时间:11/17/2023 访问量:40
Tkinter 全屏显示图像
Tkinter displaying an image on fullscreen
问:
我正在尝试为菜单设置背景图像。但是,该图像带有一些我无法完全去除的白色填充物。
图像效果很好,它是我放置在屏幕上的每个元素。
我正在寻找一种方法来去除这个填充物。或者,换句话说,让图像适合整个屏幕。
这是我的代码
from tkinter import Tk, Label, Button, Menu, PhotoImage
window = Tk()
window.state('zoomed')
color1 = '#020f12'
color2 = '#05d7ff'
color3 = '#65e7ff'
color4 = 'BLACK'
background = PhotoImage(file = r'C:\Users\acout\OneDrive - Activos Reais\Uni\Git\Tkinter\cars_background.png')
background_label= Label(window, image=background)
background_label.place(x=0, y=0, relwidth=1, relheight=1)
def configure_window():
window.geometry("800x600") # this might interfere with window.state zoomed idk
window.configure(background='#b3ffff')
window.title("My Tkinter game yo - car go brrr")
window.iconbitmap(default = r'C:\Users\acout\OneDrive - Activos Reais\Uni\Git\Tkinter\carro.ico')
window.columnconfigure(0, weight=1)
window.columnconfigure(1, weight=1)
window.rowconfigure(0, weight=1)
window.rowconfigure(1, weight=1)
window.rowconfigure(2, weight=1)
答:
0赞
acw1668
11/17/2023
#1
为了调整图像大小以适合窗口,您需要一个库,例如每当调整窗口(或包含图像的标签)大小时调整图像大小:Pillow
...
from PIL import Image, ImageTk
...
# load the image
background = Image.open('C:/Users/acout/OneDrive - Activos Reais/Uni/Git/Tkinter/cars_background.png')
background_label= Label(window)
background_label.place(x=0, y=0, relwidth=1, relheight=1)
# function to resize the image
def resize_background_image(event):
resized = background.resize((event.width, event.height))
tkimg = ImageTk.PhotoImage(resized)
background_label.configure(image=tkimg)
# use an attribute to save the reference of image to avoid garbage collection
background_label.image = tkimg
# call the resize function when the label is resized
background_label.bind("<Configure>", resize_background_image)
...
评论