4

I am using following code and trying to center the text on labels:

from tkinter import * 
root = Tk()
llist = ["first:","second label:","a really long label:","fourth:","fifth:"]
nrow = 0
for i in range(len(llist)):
    # Label(root, text=llist[i], justify='right').grid(row=nrow, column=0) # does not work
    # Label(root, text=llist[i], justify=RIGHT).grid(row=nrow, column=0) # does not work
    # Label(root, text=llist[i], anchor=E).grid(row=nrow, column=0) # does not work
    # Label(root, text=llist[i], anchor=E, justify=RIGHT).grid(row=nrow, column=0) # does not work
    Label(root, text=llist[i]).grid(row=nrow, column=0, sticky=E) # WORKS; 
    Entry(root).grid(row=nrow, column=1)
    nrow += 1
root.mainloop()

The text remains in center with options that I mention as not working in above code:

enter image description here

Only with sticky option in the grid() it works properly:

enter image description here

Why justify and anchor options of Label do not work in above code, even though they are mentioned at several places such as http://effbot.org/tkinterbook/label.htm ? How can justify and anchor be used to align text in Labels?

rnso
  • 23,686
  • 25
  • 112
  • 234
  • The problem is probably that when the widget is fit, its text's justification can be considered _anything_ as there's no space to align to. By default widgets are fit. – Nae Feb 26 '18 at 07:49
  • How are justify and anchor to be used? – rnso Feb 26 '18 at 07:52
  • The way second image works is that it first crops as much space needed for the Label, and then puts it to the east of the first column. With justify you should occupy the first column entirely with the Label widget, and simply move its text with justify option inside the label widget. Perhaps add `bg='red'` option to better see what space the Label occupies. – Nae Feb 26 '18 at 08:01
  • Possible duplicate of [how to justify text in label in tkinter in python Need justify in tkinter](https://stackoverflow.com/questions/37318060/how-to-justify-text-in-label-in-tkinter-in-python-need-justify-in-tkinter) – Nae Feb 26 '18 at 08:45
  • 2
    Add `background="pink"` to your labels. You'll be able to better visualize what is happening. – Bryan Oakley Feb 26 '18 at 13:17

1 Answers1

4

This is because you haven't set width for Label, if you don't set it, Label will fit its content. So in short, the text inside Label is worked with anchor option, but Label's length is the same as text's length. And what located at center is not text but Label.

If you manually set a width for it:

Label(root, text=llist[i], anchor="e", width=20).grid(row=nrow, column=0)

It will work, but it is difficult to manually specify the length of Label, so use grid's sticky option is much better.

Sraw
  • 18,892
  • 11
  • 54
  • 87
  • What does "it is difficult to manually specify the length of `Label`" mean? Why is it difficult? – Bryan Oakley Feb 26 '18 at 18:15
  • @BryanOakley Because in most of complicated cases, you cannot decide width easily. In this simple case, surely you can set width to `max([len(text) for text in llist])` – Sraw Feb 27 '18 at 01:09