是否可以在 Tkinter/ttk 中制作 'dynamically' 可调整的小部件

Is it possible to make 'dynamically' adjustable widgets in Tkinter/ttk

我正在为我的数据库开发非常简单的 GUI。它在左侧面板的数据库中显示记录 list/tree,并且(如果用户单击某个记录)在右侧面板上显示记录。

这里是一些创建 GUI 的代码

from Tkinter import *
import ttk


master = Tk()

reclist = ttk.Treeview(columns=["TIME STAMP","HASH","MESSAGE"])
ysb = ttk.Scrollbar(orient=VERTICAL,   command= reclist.yview)
xsb = ttk.Scrollbar(orient=HORIZONTAL, command= reclist.xview)
reclist['yscroll'] = ysb.set
reclist['xscroll'] = xsb.set
reclist.grid(in_=master, row=0, column=0,  sticky=NSEW)
ysb.grid(in_=master, row=0, column=1, sticky=NS)
xsb.grid(in_=master, row=1, column=0, sticky=EW)

Comment = Text(master)
Comment.tag_configure("center", justify='center')
ysc = ttk.Scrollbar(orient=VERTICAL,   command= Comment.yview)
xsc = ttk.Scrollbar(orient=HORIZONTAL, command= Comment.xview)
Comment.grid(in_=master,row=0,column=2,sticky=W+E+N+S)#, columnspan=5)
ysc.grid(in_=master, row=0, column=3, sticky=NS)
xsc.grid(in_=master, row=1, column=2, sticky=EW)
master.rowconfigure(0, weight=3)
master.columnconfigure(0, weight=3)
master.columnconfigure(2, weight=3)

master.mainloop()

一切都很好,除了两个面板不可调节。我不能移动它们之间的边界来使记录列表或记录面板变大或变小。我很确定这是可能的(例如,在 gitk 中,您可以移动提交列表和显示的提交之间的边界)。我搜索了很多都没有运气。

您要找的是 "PanedWindow"。 tkinter 和 ttk 模块都有一个,它们的工作方式几乎相同。一般的想法是创建一个 PanedWindow 实例,然后向其中添加两个或更多小部件。 PanedWindow 将在每个小部件之间添加一个可移动的滑块。通常你会使用框架,然后你可以用其他小部件填充它。

这是一个使用 Tkinter 中的示例:

import Tkinter as tk

root = tk.Tk()

pw = tk.PanedWindow()
pw.pack(fill="both", expand=True)

f1 = tk.Frame(width=200, height=200, background="bisque")
f2 = tk.Frame(width=200, height=200, background="pink")

pw.add(f1)
pw.add(f2)

# adding some widgets to the left...
text = tk.Text(f1, height=20, width=20, wrap="none")
ysb = tk.Scrollbar(f1, orient="vertical", command=text.yview)
xsb = tk.Scrollbar(f1, orient="horizontal", command=text.xview)
text.configure(yscrollcommand=ysb.set, xscrollcommand=xsb.set)

f1.grid_rowconfigure(0, weight=1)
f1.grid_columnconfigure(0, weight=1)

xsb.grid(row=1, column=0, sticky="ew")
ysb.grid(row=0, column=1, sticky="ns")
text.grid(row=0, column=0, sticky="nsew")

# and to the right...
b1 = tk.Button(f2, text="Click me!")
s1 = tk.Scale(f2, from_=1, to=20, orient="horizontal")

b1.pack(side="top", fill="x")
s1.pack(side="top", fill="x")

root.mainloop()