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

如何移除Tkinter窗口中的空白区域?

解决Tkinter窗口空白区域问题

你设置了固定的500x500窗口尺寸,但当前仅包含一个Label组件,未被填充的空间就显示为空白。以下两种方案可解决这个问题:

方案1:让窗口自动适配内容

删除固定窗口尺寸的代码,让Tkinter根据组件大小自动调整窗口,避免多余空白:

import tkinter as tk

root = tk.Tk()
root.title("Shop")
root['background'] = '#fbf5df'

label = tk.Label(root, text="Welcome to Our Shop! ", font=('Satoshi', 15))
label.pack(padx=20, pady=20)

root.mainloop()

方案2:保留固定窗口大小,让内容填充窗口

如果需要维持500x500的窗口尺寸,修改Label的pack参数,使其填充整个窗口空间,同时设置Label背景色与窗口一致:

import tkinter as tk

root = tk.Tk()
root.title("Shop")
root.geometry("500x500")
root['background'] = '#fbf5df'

# 添加bg参数匹配窗口背景,避免Label区域显示白色
label = tk.Label(root, text="Welcome to Our Shop! ", font=('Satoshi', 15), bg='#fbf5df')
# fill=tk.BOTH让Label填充水平和垂直空间,expand=True允许Label随窗口拉伸
label.pack(padx=20, pady=20, fill=tk.BOTH, expand=True)

root.mainloop()

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.06 14:00:33