Python 是否有可能使';动态';Tkinter/ttk中的可调整小部件
我正在为我的数据库开发非常简单的GUI。它在左面板上以DB的形式显示记录的列表/树,并且(如果用户单击某些记录)在右面板上显示记录 这里是一些创建GUI的代码Python 是否有可能使';动态';Tkinter/ttk中的可调整小部件,python,tkinter,widget,ttk,Python,Tkinter,Widget,Ttk,我正在为我的数据库开发非常简单的GUI。它在左面板上以DB的形式显示记录的列表/树,并且(如果用户单击某些记录)在右面板上显示记录 这里是一些创建GUI的代码 from Tkinter import * import ttk master = Tk() reclist = ttk.Treeview(columns=["TIME STAMP","HASH","MESSAGE"]) ysb = ttk.Scrollbar(orient=VERTICAL, command= reclist.
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()
除了两个面板不可调之外,其他一切都很好。我不能移动它们之间的边界来使记录列表或记录面板变大或变小。我非常确定in是可能的(例如,在gitk中,您可以移动提交列表和已显示提交之间的边界)。我已经搜索了很多次,但都没有找到运气。你要找的东西叫做“平移窗口”。tkinter和ttk模块都有一个,它们的工作方式几乎相同。一般的想法是创建一个
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()
你要找的东西叫做“平移窗口”。tkinter和ttk模块都有一个,它们的工作方式几乎相同。一般的想法是创建一个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()
非常感谢。你能把你的例子扩展到小部件吗?我已经让它工作了,但是框架覆盖,并且不会随着滑动而改变小部件的大小。@rth:好的,我在两侧都添加了一些小部件。谢谢!你能把你的例子扩展到小部件吗?我已经让它工作了,但是框架覆盖,并且不会随着滑动而改变小部件的大小。@rth:好的,我在两侧都添加了一些小部件。