You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Tkinter全屏显示图片问题:移除背景图白边以适配全屏

解决Tkinter背景图片白色边距问题

问题原因

出现白色边距主要是两个问题导致:

  • 窗口默认自带高亮边框和基础边框,会露出空隙
  • 背景Label本身有默认边距,再加上窗口设置了背景色,会透过空隙显示出来

修复步骤

1. 移除窗口默认边框

创建窗口时直接关闭边框相关属性,避免和全屏状态冲突:

window = Tk()
window.state('zoomed')
# 关掉窗口的高亮边框和基础边框
window.config(highlightthickness=0, borderwidth=0)

2. 优化背景Label配置

给背景Label也关掉边框属性,确保完全贴合窗口:

background_label = Label(window, image=background, borderwidth=0, highlightthickness=0)
background_label.place(x=0, y=0, relwidth=1, relheight=1)

3. 修正窗口配置函数

删掉会和全屏状态冲突的固定尺寸设置,同时移除多余的窗口背景色(图片已经覆盖全屏,没必要留),别忘了调用这个配置函数:

def configure_window():
    # 去掉800x600的固定尺寸,避免和zoomed全屏冲突
    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)

# 必须调用这个函数才会生效
configure_window()

完整修复后的代码

from tkinter import Tk, Label, Button, Menu, PhotoImage

window = Tk()
window.state('zoomed')
window.config(highlightthickness=0, borderwidth=0)

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, borderwidth=0, highlightthickness=0)
background_label.place(x=0, y=0, relwidth=1, relheight=1)

def configure_window():
    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)

configure_window()

window.mainloop()

额外提醒

如果做完上面的操作还有白边,那大概率是图片本身自带白色边缘,用图片编辑工具裁剪掉就行。

内容的提问来源于stack exchange,提问作者António Rebelo

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.06 02:15:21