【问题标题】:Tkinter and Python3 - How Can I justify a multiple lines text in a label widget?Tkinter 和 Python3 - 如何在标签小部件中证明多行文本的合理性?
【发布时间】:2020-05-27 00:02:03
【问题描述】:

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

【问题讨论】:

  • 您可以在单词之间插入空格以将每行扩展到所需的宽度。
  • ciao acw1668!我可以,但这是一种解决方法,而不是解决方案。此外,它不可扩展。如果存在,我更愿意使用已经集成在 tkinter 中的自动解决方案。
  • 如您所说,Label 小部件中没有此功能,那么您应该考虑其他选项。
  • 上网我找不到合适的解决方案。也许社区的其他人可以帮助我们。

标签: python tkinter


【解决方案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()

加上tkinter.filedialog,你就可以做一个真正的记事本了!

Python 就是这样设计的。

【讨论】:

    猜你喜欢
    • 2014-02-24
    • 2011-08-23
    • 1970-01-01
    • 2017-06-12
    • 1970-01-01
    • 1970-01-01
    • 2021-07-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多