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

tkinter设置背景图片填充整个窗口时侧边出现空白问题求助

问题原因
  • Image.resize() 参数顺序错误:Pillow的resize方法接收参数为(目标宽度, 目标高度),原代码将屏幕高度作为图片宽度传入,又固定设置1800作为高度,与窗口尺寸不匹配。
  • Label布局填充配置错误:pack()仅配置fill=Y只会让标签垂直填充,水平方向未适配导致侧边出现空白。
修复后代码
from tkinter import *
from PIL import ImageTk, Image


# 初始化窗口
ws = Tk()
ws.title("window")

# 获取屏幕宽高
width = ws.winfo_screenwidth()
height = ws.winfo_screenheight()

# 设置窗口为全屏且不可缩放
ws.geometry("%dx%d" % (width, height))
ws.resizable(False, False)

# 读取图片并按窗口尺寸缩放
imgTemp = Image.open("images/wallpaper.png")
# 修正resize参数顺序,匹配窗口宽高
img2 = imgTemp.resize((width, height))
img = ImageTk.PhotoImage(img2)

label = Label(ws, image=img)
# 修正填充配置,双向填充覆盖整个窗口
label.pack(fill=BOTH, expand=True)

# 其余组件代码
# text = Text(ws,height=10,width=53)
# text.place(x=30, y=50)
# button = Button(ws,text='SEND',relief=RAISED,font=('Arial Bold', 18))
# button.place(x=190, y=250)

ws.mainloop()
问题原始示例图

示例图片

内容的提问来源于stack exchange,提问作者Bous

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.26 19:06:09