使用自动换行创建可调整大小的/多行 Tkinter/ttk 标签 [英] Create resizable/multiline Tkinter/ttk Labels with word wrap

查看:32
本文介绍了使用自动换行创建可调整大小的/多行 Tkinter/ttk 标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以创建一个自动换行的多行标签,并与其父标签的宽度同步调整大小?换句话说,当您更改记事本窗口的宽度时,记事本的自动换行行为.

Is it possible to create a multi-line label with word wrap that resizes in sync with the width of its parent? In other words the wordwrap behavior of Notepad as you change the width of the NotePad window.

用例是一个对话框,它需要完整地呈现多行文本(指令)块,而不需要剪切文本或求助于滚动条.父容器将有足够的垂直空间来容纳较窄的宽度.

The use case is a dialog that needs to present a block of multi-line text (instructions) in its entirety without having the text clipped or resorting to scrollbars. The parent container will have enough vertical space to accomodate narrow widths.

我一直在尝试使用 Tkinter Label 和 Message 小部件以及 ttk Label 小部件,但没有成功.似乎我需要硬编码一个像素环绕长度值,而不是让这些控件在文本到达其容器的右边缘时自动换行.Tkinters 几何管理器当然可以帮助我自动调整标签大小并相应地更新它们的包裹长度值?

I've been experimenting with Tkinter Label and Message widgets and the ttk Label widget without success. It seems that I need to hard code a pixel wraplength value vs. have these controls auto wordwrap when their text reaches the right edge of their containers. Certainly Tkinters geometry managers can help me auto-resize my labels and update their wraplength values accordingly?

我应该查看文本小部件吗?如果是这样,是否可以隐藏文本小部件的边框,以便我可以将其用作带有自动换行的多行标签?

Should I be looking at the Text widget instead? If so, is it possible to hide the border of a Text widget so I can use it as a multi-line label with wordwrap?

这是一个如何做我上面描述的事情的原型.它的灵感来自 Bryan Oakley 使用 Text 小部件的提示和 Stackoverflow 上的以下帖子:在python的tkinter中,我如何制作一个标签,以便您可以用鼠标选择文本?

Here's a prototype of how one might do what I described above. It was inspired by Bryan Oakley's tip to use the Text widget and the following post on Stackoverflow: In python's tkinter, how can I make a Label such that you can select the text with the mouse?

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()

推荐答案

不,Tk 没有内置功能来自动换行标签.但是,它可以通过绑定到标签的 事件并调整包装长度来实现.每次调整标签小部件的大小时都会触发此绑定.

No, there is no feature built-in to Tk to auto-word-wrap labels. However, it's doable by binding to the <Configure> event of the label and adjusting the wrap length then. This binding will fire every time the label widget is resized.

正如您所建议的,另一个选项是使用文本小部件.如果您愿意,可以完全关闭边界.当我想要自动换行的教学文本时,这一直是我的选择.

The other option, as you suggest, is to use a text widget. It is possible to entirely turn off the border if you so desire. This has always been my choice when I want word-wrapped instructional text.

这篇关于使用自动换行创建可调整大小的/多行 Tkinter/ttk 标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆