是否可以使用自动换行创建多行标签,该标签的大小与其父级宽度同步?换句话说,当您更改记事本窗口的宽度时,记事本的换字行为。
用例是一个对话框,它需要完整地呈现多行文本(指令)块,而无需剪裁文本或求助于滚动条。父容器将有足够的垂直空间来容纳窄宽度。
我一直在尝试Tkinter标签和消息小部件以及ttk标签小部件,但没有成功。似乎我需要硬编码一个像素wrapeningth值,而当这些控件的文本到达容器的右边缘时,它们会自动换行。当然,Tkinters几何管理器可以帮助我自动调整标签的大小并相应地更新它们的wrapength值?
我应该改为查看文本小部件吗?如果是,是否可以隐藏文本小部件的边框,以便我可以将其用作带换行符的多行标签?from Tkinter import *
master = Tk()
text = """
If tkinter is 8.5 or above you'll want the selection background to appear like it does when the widget is activated. Comment this out for older versions of Tkinter.
This is even more text.
The final line of our auto-wrapping label that supports clipboard copy.
""".strip()
frameLabel = Frame( master, padx=20, pady=20 )
frameLabel.pack()
w = Text( frameLabel, wrap='word', font='Arial 12 italic' )
w.insert( 1.0, text )
w.pack()
# - have selection background appear like it does when the widget is activated (Tkinter 8.5+)
# - have label background color match its parent background color via .cget('bg')
# - set relief='flat' to hide Text control borders
# - set state='disabled' to block changes to text (while still allowing selection/clipboard copy)
w.configure( bg=master.cget('bg'), relief='flat', state='disabled' )
mainloop()