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

Tkinter网格布局出现多余空白,如何高效使用grid?

Tkinter网格布局多余空白问题修复

问题现象

输入图片描述

  • 'Button 1'和'Entry 1'共享第0行第0列单元格,单元格尺寸与位置动态变化
  • 'Button 2'和'Button 3'同理

存在两个问题:

  1. 'Entry 1'未填满所在单元格空间
  2. 第0行0列的空白会被添加到第0行1列

原代码

import tkinter as tk
from tkinter import *

root = tk.Tk()

root.geometry()
root.title('Test-Window')

button1 = Button(root, text='Button 1')
button1.grid(sticky='W', row=0, column=0)

button1.update()
entry1_padx = button1.winfo_width() + 1

entry1 = Entry(root)
entry1.grid(padx=entry1_padx, row=0, column=0)

button2 = Button(root, text='Button 2')
button2.grid(sticky='W', row=0, column=1)

button2.update()
button3_padx = button2.winfo_width() + 1

button3 = Button(root, text='Button 3')
button3.grid(padx=button3_padx, sticky='W', row=0, column=1)

label1 = Label(root, text='this is were the cell ends')
label1.grid(sticky="E", row=1, column=0)

root.mainloop()

问题原因

原代码通过padx偏移控件位置,导致grid单元格被额外内边距撑大,进而产生跨列的多余空白;同时Entry未设置填充属性,无法占满单元格剩余空间。

修复方案(推荐):使用Frame作为容器

将每个grid单元格内的多个控件放入Frame中,通过Frame的布局控制内部控件,同时让Frame占满整个grid单元格,避免空白问题。

import tkinter as tk
from tkinter import *

root = tk.Tk()
root.title('Test-Window')

# 配置窗口列权重,让列随窗口拉伸
root.columnconfigure(0, weight=1)
root.columnconfigure(1, weight=1)

# 第0列容器:放置Button1和Entry1
frame_col0 = Frame(root)
frame_col0.grid(row=0, column=0, sticky="nsew")  # Frame占满单元格
frame_col0.columnconfigure(1, weight=1)  # 让Frame内部第1列拉伸

button1 = Button(frame_col0, text='Button 1')
button1.grid(sticky='W', row=0, column=0)

entry1 = Entry(frame_col0)
# Entry填充剩余空间,左侧与Button1留1px间隙
entry1.grid(sticky="ew", row=0, column=1, padx=(1, 0))

# 第1列容器:放置Button2和Button3
frame_col1 = Frame(root)
frame_col1.grid(row=0, column=1, sticky="nsew")

button2 = Button(frame_col1, text='Button 2')
button2.grid(sticky='W', row=0, column=0)

button3 = Button(frame_col1, text='Button 3')
# Button3与Button2左侧留1px间隙
button3.grid(sticky='W', row=0, column=1, padx=(1, 0))

label1 = Label(root, text='this is where the cell ends')
label1.grid(sticky="E", row=1, column=0)

root.mainloop()

方案说明

  1. 用Frame作为每个grid单元格的容器,通过sticky="nsew"让Frame完全填充单元格
  2. 配置Frame内部的列权重,让Entry所在列自动拉伸,占满剩余空间
  3. 控件间的间隙通过padx直接设置,不会影响外层grid单元格的尺寸,彻底解决跨列空白问题

备选方案:使用place定位控件

直接通过place精确控制每个控件的位置,避免grid内边距导致的单元格撑大问题,适合需要更精细定位的场景。

import tkinter as tk
from tkinter import *

root = tk.Tk()
root.title('Test-Window')

root.columnconfigure(0, weight=1)
root.columnconfigure(1, weight=1)

button1 = Button(root, text='Button 1')
button1.place(row=0, column=0, anchor="w")

entry1 = Entry(root)
# Entry从Button1右侧开始,填充到单元格最右侧
entry1.place(row=0, column=0, relx=0, rely=0, relwidth=1, x=button1.winfo_width()+1, anchor="nw")

button2 = Button(root, text='Button 2')
button2.place(row=0, column=1, anchor="w")

button3 = Button(root, text='Button 3')
button3.place(row=0, column=1, x=button2.winfo_width()+1, anchor="w")

label1 = Label(root, text='this is where the cell ends')
label1.grid(sticky="E", row=1, column=0)

# 窗口大小变化时,重新调整Entry宽度
def adjust_entry_width(event):
    entry1.place_configure(
        x=button1.winfo_width()+1,
        relwidth=1 - (button1.winfo_width()+1)/event.width
    )

root.bind("<Configure>", adjust_entry_width)

root.mainloop()

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.17 22:32:05