Python Forum
[Tkinter] Align the width of the entrys with the column captions
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tkinter] Align the width of the entrys with the column captions
#1
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()
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  align frame inside canvas ro_btz 1 1,852 Sep-11-2020, 04:38 PM
Last Post: Larz60+
  What units does the width parameter of a tkinter Listbox use? Pedroski55 2 5,598 Jul-04-2020, 08:17 AM
Last Post: deanhystad
  [Tkinter] Label, align imported text from pandas kundrius 2 4,234 Dec-11-2019, 08:26 AM
Last Post: kundrius
  Canvas Text Width/Height Anysja 0 5,177 Aug-20-2018, 11:11 PM
Last Post: Anysja

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020