ttk.Treeview小部件允许您创建具有多列的对象列表。它可能是您最容易使用的东西。
由于您特别询问了标签网格,这里有一个快速而肮脏的示例,展示了如何在可滚动网格中创建 300 个项目:
import Tkinter as tk
class ExampleApp(tk.Tk):
def __init__(self):
tk.Tk.__init__(self)
# create a canvas to act as a scrollable container for
# the widgets
self.container = tk.Canvas(self)
self.vsb = tk.Scrollbar(self, orient="vertical", command=self.container.yview)
self.container.configure(yscrollcommand=self.vsb.set)
self.vsb.pack(side="right", fill="y")
self.container.pack(side="left", fill="both", expand=True)
# the frame will contain the grid of labels and values
self.frame = tk.Frame(self)
self.container.create_window(0,0, anchor="nw", window=self.frame)
self.vars = []
for i in range(1,301):
self.vars.append(tk.StringVar(value="This is the value for item %s" % i))
label = tk.Label(self.frame, text="Item %s:" % i, width=12, anchor="e")
value = tk.Label(self.frame, textvariable=self.vars[-1], anchor="w")
label.grid(row=i, column=0, sticky="e")
value.grid(row=i, column=1, sticky="ew")
# have the second column expand to take any extra width
self.frame.grid_columnconfigure(1, weight=1)
# Let the display draw itself, the configure the scroll region
# so that the scrollbars are the proper height
self.update_idletasks()
self.container.configure(scrollregion=self.container.bbox("all"))
if __name__ == "__main__":
app = ExampleApp()
app.mainloop()