How to set justification on Tkinter Text box

You’re using an option named justify, but no such option exists for the text widget. Are you reading some documentation somewhere that says justify is a valid option? If so, you need to stop reading that documentation.

There is, however, a justify option for text tags. You can configure a tag to have a justification, then apply that tag to one or more lines. I’ve never used a ScrolledText widget so I don’t know if it has the same methods as a plain text widget, but with a plain text widget you would do something like this:

t = tk.Text(...)
t.tag_configure("center", justify='center')
t.tag_add("center", 1.0, "end")

In the above example, we are creating and configuring a tag named “center”. Tags can have any name that you want. For the “center” tag we’re giving it a justification of “center”, and then applying that tag to all the text in the widget.

You can tag individual lines by adjusting the arguments to the tag_add method. You can also give a list of tags when inserting text using the insert method.

Leave a Comment