如何在 tkinter 中将文本的对齐方式设置为 "Justify"?

How to set the justification of the text to "Justify" in tkinter?

我正在尝试证明来自 tkinter 文本的文本。其他答案(How to set justification on Tkinter Text box)说是我的答案。我正在尝试使用此代码(我正在使用 tkinter 8.6 和 Python 3):

import tkinter
root = tkinter.Tk()
text_widget = tkinter.Text()
text_widget.pack(fill='both', expand=True)
text_widget.tag_configure('tag-center', justify='justify')
text_widget.insert('end', 'text ' * 10, 'tag-center')

但是,如果我 运行 代码:

Traceback (most recent call last):
  File "C:/Users/moon/AppData/Local/Programs/Python/Python310/asking1.py", line 5, in <module>
    text_widget.tag_configure('tag-center', justify='justify')
  File "C:\Users\moon\AppData\Local\Programs\Python\Python310\lib\tkinter\__init__.py", line 3888, in tag_configure
    return self._configure(('tag', 'configure', tagName), cnf, kw)
  File "C:\Users\moon\AppData\Local\Programs\Python\Python310\lib\tkinter\__init__.py", line 1665, in _configure
    self.tk.call(_flatten((self._w, cmd)) + self._options(cnf))
_tkinter.TclError: bad justification "justify": must be left, right, or center

它说“_tkinter.TclError:错误的理由“证明”:必须leftright,或者center,但是没有justify。我看到 MS Word 和 LO Writer 都有 Justify.怎么办?谢谢!

Please click this link: Word Justification

遗憾的是,Tkinter 文本只有三个对齐选项:leftcenter对齐。但不要失望,您可以尝试使用名为 spacing2 的东西,并通过使用标签和选项配置来发现它。试试吧!

P.S。你也可以试试 PyQt5。他们可能有理由选择。 wxPython也是不错的选择

em 实际上 tkinter 中有 NO justify 函数。它只有 "left" (tk.LEFT), "center" (tk.CENTER)"right" (tk.RIGHT)

但不要失望,就像 jason 先生说的 pyqt5 有以下证明选项:

import PyQt5.QtCore
# import PySide5.QtCore

PyQt5.QtCore.Qt.AlignLeft      # PySide6.QtCore.Qt.AlignLeft
PyQt5.QtCore.Qt.AlignHCenter   # PySide6.QtCore.Qt.AlignHCenter
PyQt5.QtCore.Qt.AlignRight     # PySide6.QtCore.Qt.AlignRight
PyQt5.QtCore.Qt.AlignJustify   # PySide6.QtCore.Qt.AlignJustify
PyQt5.QtCore.Qt.AlignTop       # PySide6.QtCore.Qt.AlignTop
PyQt5.QtCore.Qt.AlignBottom    # PySide6.QtCore.Qt.AlignBottom
PyQt5.QtCore.Qt.AlignVCenter   # PySide6.QtCore.Qt.AlignVCenter
PyQt5.QtCore.Qt.AlignBaseline  # PySide6.QtCore.Qt.AlignBaseline

那为什么不愉快的使用pyqt5做gui应用呢? :)