0

I am trying to create a simple table using Tkinter. However the label and the text widgets do not align horizontally. This is for example my Line_number label :

    Line_Number_label = Label(Frame_Label_Menu, text="N°", anchor="w", bg="#ffffff", width=3)
    Line_Number_label.grid(column=0, row=0, sticky=E+W)

This is my text label, which gets the value entered by the user in an entry field:

    Text0 = Text(Frame_Text, height=1, width=3)
    Text0.grid(column=0, row=Line_number, sticky=E+W)
    Text0.insert("end", Line_number)

The winfo.width() function returns a length of 30 pixels on Line_Number_label and of 25 pixels on Text0. I gave all widgets the same police and the same size:

    root.option_add("*Font", "Roboto 10")

For some reason the width of the label widgets is always slightly longer than the text widgets:

enter image description here

Label widgets do not seem to set their dimensions ("width") on the number of characters as text widgets do. Or am I doing something wrong ?

halfer
  • 19,824
  • 17
  • 99
  • 186
  • Does this answer your question? [Set the same width to an entry and text tkinter label](https://stackoverflow.com/questions/16645990/set-the-same-width-to-an-entry-and-text-tkinter-label) – David Duran Jul 25 '20 at 11:24
  • 1
    The easiest way to achieve your goal is to use `pack` and make each widget `fill="x"`. – David Duran Jul 25 '20 at 11:25
  • Please [edit] your question to include a [mcve] that illustrates the problem you are having. Also, label widgets _do_ set their dimensions to the number of characters, unless they have an image. – Bryan Oakley Jul 25 '20 at 13:54
  • Thank you everyone for your comments. I am quite new to the exiting world of programming and also to stackoverflow. This is the second time that I have asked a question and I really enjoy your constructive responses ! I will look into this minimal reproducible example. – Sébastien A. Jul 25 '20 at 20:52
  • Try using a fixed width font, like `Consolas` or `Courier`, and setting `padx=0` for labels. – acw1668 Jul 26 '20 at 04:35

1 Answers1

0

The following code makes sure that your columns are aligned. It is just an example with 2 columns, but it is extensible to more columns:

# Import requrired libraries/packages
from tkinter import Tk, Frame, X, Y, TOP, Label, Text, LEFT

# Create intance of tkinter
root = Tk(className = 'Python Examples - Window 0')
root.geometry("600x700")
root.resizable(0,0)

# Frame for whole table
frame_table=Frame(root, highlightthickness=0)
frame_table.pack(side=TOP,fill=X)

# Line number
Line_number=1

# 1st column
frame_col1 = Frame(frame_table, highlightthickness=0, width=3)
Line_Number_label = Label(frame_col1, text="N°", anchor="w", bg="#ffffff", width=3)
Text0 = Text(frame_col1, height=1, width=3)
Line_Number_label.pack(fill=X)
Text0.pack(fill=X)

# 2nd column
frame_col2 = Frame(frame_table, highlightthickness=0, width=20)
Line_Date_label = Label(frame_col2, text="Eingangsdatum", anchor="w", bg="#ffffff")
Text1 = Text(frame_col2, height=1)
Line_Date_label.pack(fill=X)
Text1.pack(fill=X)

# Packing of columns
frame_col1.pack(side=LEFT, fill=Y)
frame_col2.pack(side=LEFT, fill=Y)

root.mainloop()

I have used pack because I have had some problems with grid in the past. Packing like this is more stable to me.

David Duran
  • 1,786
  • 1
  • 25
  • 36