Marek
Marek

Reputation: 943

How to make Tkinter.Label justify correctly

I am having an issue getting the text inside a label to left justify in Tkinter. I have tried using the justify=Tkconstants.LEFT but that doesn't seem to work. I have also tried anchor=Tkcontants.W but that doesn't seem to help either. The text always appears in the center of the label. Here is what I have regarding the label:

LookupOutput = Tkinter.Label(UI,textvariable=User,justify=Tkconstants.LEFT)
LookupOutput.grid(row=5, column=0, columnspan=5, padx=5)

I assume it has something to do with my columnspan but I don't know how to fix it. Does anyone have an idea on how to get this thing to left justify the text?

Upvotes: 1

Views: 5836

Answers (1)

Bryan Oakley
Bryan Oakley

Reputation: 385870

The proper way to have the text in a label aligned to the left of the widget is with the anchor attribute. A value of "w" (or by using the Tkinter constant W) stands for "west".

LookupOutput = Tkinter.Label(UI,textvariable=User,anchor=Tkconstants.W)

The justify attribute only affects text that wraps.

If that isn't working, the problem might not be with the text in the label. It might be that your label doesn't occupy the space you think it does. An easy way to check this is to give the label a distinctive background color so that you can distinguish the label from its parent.

I notice that you aren't using the sticky attribute when you call grid -- that may also cause problems. By default grid will align a widget in the center of the space allocated to it. If you want it to "stick" to the left side of the space that was given to it, use sticky="w" (or, again, use the Tkinter constant).

Upvotes: 2

Related Questions