Python Tkinter栅格填充空白空间

Python Tkinter栅格填充空白空间,python,tkinter,grid-layout,Python,Tkinter,Grid Layout,在发布之前,我确实搜索了很多例子,但仍然不能正确使用tkinter网格 我想要什么: 我的代码: import tkinter as tk from tkinter import ttk root = tk.Tk() b1 = ttk.Button(root, text='b1') b1.grid(row=0, column=0, sticky=tk.W) e1 = ttk.Entry(root) e1.grid(row=0, column=1, sticky=tk.EW) t = t

在发布之前,我确实搜索了很多例子,但仍然不能正确使用tkinter网格

我想要什么:

我的代码:

import tkinter as tk
from tkinter import ttk

root = tk.Tk()

b1 = ttk.Button(root, text='b1')
b1.grid(row=0, column=0, sticky=tk.W)

e1 = ttk.Entry(root)
e1.grid(row=0, column=1, sticky=tk.EW)

t = ttk.Treeview(root)
t.grid(row=1, column=0, sticky=tk.NSEW)

scroll = ttk.Scrollbar(root)
scroll.grid(row=1, column=1, sticky=tk.E+tk.NS)

scroll.configure(command=t.yview)
t.configure(yscrollcommand=scroll.set)

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

root.mainloop()

快速而简单的解决方案是定义
treeview
列span
。这将告诉treeview在两列中展开,并允许输入字段位于按钮旁边

在一个不相关的注释中,您可以将字符串用于
粘性
,这样您就不必执行
tk.E+tk.NS
之类的操作。相反,只需使用
“nse”
或任何您需要的指示即可。确保您按照
“nsew”
的顺序进行操作

结果:

要解决您在评论中提到的问题,您可以删除
root.columnconfigure(0,weight=1)
,以使条目正确展开


t.grid(行=1,列=0,粘性=tk.NSEW)更改为
t.grid(行=1,列=0,列span=2,粘性=tk.NSEW)
。谢谢,这修复了树状视图和滚动条,但在调整窗口大小时,条目仍有空白。是否希望条目字段随窗口一起扩展?它已随窗口一起扩展到右侧,但距离按钮太远。这是因为您在第0行上设置了权重。您可以通过删除
root.columnconfigure(0,weight=1)
来修复此问题。看到我的答案了。非常感谢你,迈克,我自己决不会做的:)@VítorNunes当然。如果我的答案解决了您的问题,请确保选中我的答案旁边的复选标记,表明您的问题已得到解决。
import tkinter as tk
from tkinter import ttk

root = tk.Tk()

b1 = ttk.Button(root, text='b1')
b1.grid(row=0, column=0, sticky="w")

e1 = ttk.Entry(root)
e1.grid(row=0, column=1, sticky="ew")

t = ttk.Treeview(root)
t.grid(row=1, column=0, columnspan=2, sticky="nsew") # columnspan=2 goes here.

scroll = ttk.Scrollbar(root)
scroll.grid(row=1, column=2, sticky="nse") # set this to column=2 so it sits in the correct spot.

scroll.configure(command=t.yview)
t.configure(yscrollcommand=scroll.set)

# root.columnconfigure(0, weight=1) Removing this line fixes the sizing issue with the entry field.
root.columnconfigure(1, weight=1)
root.rowconfigure(1, weight=1)

root.mainloop()