How to set justification on Tkinter Text box

前端 未结 4 1444
青春惊慌失措
青春惊慌失措 2020-12-02 00:51

Question

  1. How can I change the justification of specific lines in the ScrolledText widget in Tkinter?
  2. What was the reason
4条回答
  •  春和景丽
    2020-12-02 01:33

    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.

提交回复
热议问题