将条目的宽度与列标题对齐

时间:2020-04-23 08:26:00

标签: python tkinter datatable size

我创建了一个表,该表由列标题(姓,名,..)和条目组成。由于该表有几行,因此我在表的右侧添加了一个滚动条。

以下问题: 输入字段的宽度与列标题的宽度不匹配。结果,仅显示3列而不是5列。当然,您可以手动更改各个条目的宽度。但是结果看起来不是很好。相反,每列条目的宽度应与列标题的宽度对齐。

对于下面的最小示例,您是否有人知道如何实现此目的?

from tkinter import *

root = Tk()
Label(root, text = "Name").grid(row = 0, column = 0)
Label(root, text = "Vorname").grid(row = 0, column = 1)
Label(root, text = "Land").grid(row = 0, column = 2)
Label(root, text = "Stadt").grid(row = 0, column = 3)
Label(root, text = "Adresse").grid(row = 0, column = 4)

container = Frame(root)
canvas = Canvas(container, height = 190)
scrollbar = Scrollbar(container, orient="vertical", command=canvas.yview)
scrollable_frame = Frame(canvas)
scrollable_frame.bind(
    "<Configure>",
    lambda e: canvas.configure(
        scrollregion=canvas.bbox("all")
    )
)
canvas.create_window((0, 0), window=scrollable_frame, anchor="nw")
canvas.configure(yscrollcommand=scrollbar.set)
for i in range(20): # number of rows
    for j in range(5): # number of columns
        Entry(scrollable_frame).grid(row = i + 1  , column = j)

container.grid(row = 1, column = 0, columnspan = 5)
canvas.pack(side="left", fill="both", expand=True)
scrollbar.pack(side="right", fill="y")

root.mainloop()

0 个答案:

没有答案