我正在尝试从tkinter文本中调整文本的对齐方式。其他答案(How to set justification on Tkinter Text box)说这是我的答案。我正在尝试使用这段代码(我正在使用tkinter 8.6和Python 3):
但是,如果我运行这段代码:
它说:“_tkinter.TclError: 错误的对齐方式“justify”:必须是左、右或中,但没有“justify”。我发现 MS Word和 LO Writer都有Justify。怎么办?谢谢!”
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: 错误的对齐方式“justify”:必须是左、右或中,但没有“justify”。我发现 MS Word和 LO Writer都有Justify。怎么办?谢谢!”