pack有一些选项,允许您控制要使用的控件的哪一面、填充和其他内容。例如,要从左到右打包小部件,可以使用widget.pack(side='left')。所有这些选择在许多地方都有很好的文档记录。在
下面是一个快速示例,在顶部显示一行按钮,右侧显示一列标签:import Tkinter as tk
class Example(tk.Frame):
def __init__(self, parent):
tk.Frame.__init__(self, parent)
self.toolbar = tk.Frame(self, borderwidth=1, relief="raised")
self.labels = tk.Frame(self, borderwidth=1, relief="raised")
self.text = tk.Text(self, width=80, height=20)
self.toolbar.pack(side="top", fill="x")
self.labels.pack(side="right", fill="y")
self.text.pack(side="left", fill="both", expand=True)
for i in range(5):
button = tk.Button(self.toolbar, text="Button %d" %i)
button.pack(side="left")
for i in range(5):
label = tk.Label(self.labels, text="Label %d" %i)
label.pack(side="top", fill="x", padx=8)
if __name__ == "__main__":
root = tk.Tk()
Example(root).pack(fill="both", expand=True)
root.mainloop()