简体   繁体   English

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

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

I am trying to justify the text from tkinter text.我试图证明来自 tkinter 文本的文本。 The other answers ( How to set justification on Tkinter Text box )said that it is my answer.其他答案( 如何在 Tkinter 文本框上设置理由)说这是我的答案。 I am try to use this code (I am using tkinter 8.6 and Python 3):我尝试使用此代码(我正在使用 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')

but then, if I run the code:但是,如果我运行代码:

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

It said "_tkinter.TclError: bad justification "justify": must be left , right , or center , but no justify . I saw it that both MS Word and LO Writer has Justify . How to do it? Thank you!它说 "_tkinter.TclError: bad justification "justify": must be left , right , or center , 但没有justify 。我看到MS Word 和LO Writer 都有Justify 。怎么办?谢谢!

Please click this link: Word Justification请点击此链接:字对齐

Sadly, the Tkinter Text has only three justify option: left , center and right aligns.遗憾的是,Tkinter Text 只有三个对齐选项:对齐、居中对齐和对齐。 But don't be disappointed, you may try something called spacing2 and discover it by using tags and option configuration.但是不要失望,你可以尝试一些叫做spacing2的东西,并通过使用标签和选项配置来发现它。 Try it!试试看!

PS You may also try PyQt5. PS 你也可以试试 PyQt5。 They may have justify option.他们可能有理由选择。 wxPython is also a good choice. wxPython 也是一个不错的选择。

em actually there's NO justicfy function in tkinter.实际上,tkinter 中的function是没有道理的。 it has only "left" ( tk.LEFT ) , "center" ( tk.CENTER ) or "right" ( tk.RIGHT )它只有"left" tk.LEFT "center" tk.CENTER "right" tk.RIGHT

but dont be disappointed, like mr jason said pyqt5 has the following justify options:但不要失望,就像 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

so why not use pyqt5 for gui applications happily?那么为什么不愉快地将 pyqt5 用于 gui 应用程序呢? :) :)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM