Home > Enterprise >  Aligning Text in Buttons
Aligning Text in Buttons

Time:11-15

from tkinter import *

root = Tk()
Button(root, text="sldkjf", justify=LEFT, padx=130).pack()

root.mainloop()

Why doesn't the text show in the left, although I set the justify to left?

CodePudding user response:

The documentation for the justify option says this:

justify - When there are multiple lines of text displayed in a widget, this option determines how the lines line up with each other.

You don't have multiple lines, you have a single line.

To get the text to be aligned to the top, bottom, left, or right, you need to use the anchor option. This is what the documentation says:

anchor - Specifies how the information in a widget (e.g. text or a bitmap) is to be displayed in the widget. Must be one of the values n, ne, e, se, s, sw, w, nw, or center. For example, nw means display the information such that its top-left corner is at the top-left corner of the widget.

So, in your case you should use anchor="w".

You also have the problem that the excessive padding is applied to both sides, in effect squeezing the text to fit in between the 130x padding on the left and the 130px padding on the right. If you remove the padding, the text will be anchored to the left side of the widget.

  • Related