Python Forum

Full Version: Align the width of the entrys with the column captions
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I have created a table which consists of column captions (surname, first name, ..) and entries. Since the table has several rows I added a scrollbar to the right side of the table.

The following problem: The width of the entry fields does not match the width of the column captions. As a result, only 3 instead of 5 columns are displayed. Of course you can change the width of the individual entries manually. But the result does not look very nice. Instead, the widths of the entries of each column should be aligned with the width of the column captions.

Does anyone of you have an idea how to implement this for the following minimal example:

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()