如何移除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
相关产品推荐
相关产品推荐

