如何使用网格方法Python Tkinter将多个功能添加到同一个单元格中

时间:2016-08-01 19:50:53

标签: python tkinter grid

可能有这样的问题,但我找不到它。 我希望在同一个单元格中有多个条目或标签等,而不会重叠。我希望你知道我的意思。 有什么想法吗?

1 个答案:

答案 0 :(得分:0)

在框架中放置任意数量的项目,然后将框架放在网格单元格中。

实施例

import tkinter as tk

root = tk.Tk()

# some random widgets, for illustrative purposes
l0 = tk.Label(root, text="Cell 0,0", borderwidth=1, relief="solid")
l1 = tk.Label(root, text="Cell 0,1", borderwidth=1, relief="solid")
l2 = tk.Label(root, text="Cell 1,0", borderwidth=1, relief="solid")
l3 = tk.Label(root, text="Cell 1,1", borderwidth=1, relief="solid")
l4 = tk.Label(root, text="Cell 1,2", borderwidth=1, relief="solid")

# create a frame for one of the cells, and put
# a label and entry widget in it
f1 = tk.Frame(root, borderwidth=1, relief="solid")
l5 = tk.Label(f1, text="Cell 0,2")
e1 = tk.Entry(f1)

# put the label and entry in the frame:
l5.pack(side="top", fill="both", expand=True)
e1.pack(side="top", fill="x")

# put the widgets in the root
l0.grid(row=0, column=0, padx=2, pady=2, sticky="nsew")
l1.grid(row=0, column=1, padx=2, pady=2, sticky="nsew")
f1.grid(row=0, column=2, padx=2, pady=2, sticky="nsew")
l2.grid(row=1, column=0, padx=2, pady=2, sticky="nsew")
l3.grid(row=1, column=1, padx=2, pady=2, sticky="nsew")
l4.grid(row=1, column=2, padx=2, pady=2, sticky="nsew")

root.mainloop()