Tkinter 在全屏上显示图像

问题描述 投票:0回答:1

我正在尝试为菜单设置背景图像。然而,图像带有一些我无法完全删除的白色填充。

图像工作正常,它是我放置在屏幕上的每个元素。

我正在寻找一种方法来删除这个填充。或者,换句话说,让图像适合整个屏幕。

这是我的代码

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)


它看起来像这样:

python image tkinter fullscreen
1个回答
0
投票

为了调整图像大小以适合窗口,您需要像

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)
...
© www.soinside.com 2019 - 2024. All rights reserved.