Tkinter 和 Python3 - 如何调整标签小部件中的多行文本?

Tur*_*boC 5 python tkinter

我有一个由 ttk 标签中的多行组成的文本。默认情况下,文本从左边框到右边框显示,没有任何一种“对齐”选项。我的目标是将文本传播到所有标签上。我不想将文本右对齐、左对齐、居中对齐,等等。我想对齐它,尤其是当您在 Microsoft Word 中对齐文本时。我怎样才能达到我的目标?

D_0*_*_00 1

您可以使用该tkinter.ScrolledText模块:

from tkinter import *
from tkinter.scrolledtext import *

win = Tk()

TEXT="""I have a text composed by multiple lines in a ttk label.
by default the text is displayed from the left border to right one without any kind of "justify" option.
My goal is spread the text on all the label. I don't want to align the text on the right, on the left, on the center, etc..
I want to justify it, exatly when you justify a text in Microsoft Word. how can I reach my goal?"""

text = ScrolledText(win, width=80, height=41, wrap=WORD) # create text zone
text.pack(expand=True, fill=BOTH) # pack it in the entire window
text.insert(1.0, TEXT)

win.mainloop()
Run Code Online (Sandbox Code Playgroud)

通过添加tkinter.filedialog,你就可以制作一个真正的记事本了!

Python IDLE 实际上就是这样制作的。