首页 > 解决方案 > 如何使边框与子标题对齐?

问题描述

我正在尝试在tkinterusinggrid方法中创建一个类似结构的表。在我的代码中,我有 3 个标题和 3 个子标题,每个标题下面必须有三个子标题。为了实现这一点,我columnspangrid. 我几乎得到了所需的输出,但唯一缺少的是边框 ( relief = "groove") 未与其下的三个子标题对齐。

我试图改变宽度,Label但它们仍然没有对齐。

如何对齐标题和子标题边框?

from tkinter import *

root = Tk()

headers = ["header_1","header_2","header_3"]
sub_headers = ["sub_header_1","sub_header_2","sub_header_3"]

c = 0
for i in headers:
    label = Label(root, text = i, width = 15, relief = "groove")
    label.grid(row = 0, column = c, columnspan = 3)
    c += 3

c = 0
count = 1
while count < 4:
    for j in sub_headers:
        label = Label(root, text = j, width = 15, relief = "groove")
        label.grid(row = 1, column = c)
        c += 1
    count += 1

root.mainloop()

标签: python-3.xtkinter

解决方案


我相信你正在寻找sticky参数:

...

for i in headers:
    label = Label(root, text = i, width = 15, relief = "groove")
    label.grid(row = 0, column = c, columnspan = 3, sticky="ew")
    c += 3

推荐阅读