带有多行标签的 Tkinter 表单? [英] Tkinter form with multi-line labels?

查看:13
本文介绍了带有多行标签的 Tkinter 表单?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在构建一个通用的、数据驱动的 Tkinter 表单.每行左侧有一个标签,右侧有一个输入字段.对于简单的测试数据,它可以为标签文本使用标签——但是当所需的标签文本比标签字段长时,它会简单地截断.

I'm building a generic, data-driven Tkinter form. Each row has a label on the left and an input field on the right. With simple test data, it works to use a Label for the label text -- but when the desired label text is longer than the Label field, it simply truncates.

一旦构建了表单,我就不需要动态改变标签文本:文本将在构建时已知.但我不知道可能的标签字符串的范围.我希望表单通过将标签自动换行到多行并垂直扩展来容纳更长的标签文本.这当然应该扩展嵌入标签的行.

Once the form has been built, I won't need to vary the label text dynamically: the text will be known at construction time. But I don't know the universe of possible label strings. I want the form to accommodate longer label text by word-wrapping the label to multiple lines, expanding vertically. This should of course expand the row in which the label is embedded.

根据 使用自动换行创建可调整大小的/多行 Tkinter/ttk 标签,我认为我应该使用文本小部件进行自动换行和禁用它作为输入字段.但我不知道如何在水平方向限制它(以进行自动换行),同时将其垂直扩展到精确大小.

Per Create resizable/multiline Tkinter/ttk Labels with word wrap, I presume I should use a Text widget for word wrapping and disable it as an input field. But I don't know how to constrain it horizontally (to engage the word wrapping) while expanding it to exact size vertically.

换句话说,处理顺序应该是这样的:

In other words, the processing sequence should go something like this:

  1. 确定父级的总宽度.
  2. 相对于父级分配文本字段的宽度.(我想我会使用 grid_columnconfigure(weight=),但我很乐意接受建议.)
  3. 对文本内容进行自动换行以适应其宽度.
  4. 垂直展开文本块以显示所有行.
  5. 将垂直尺寸向外传播到父级.
  1. Determine the overall width of the parent.
  2. Allocate the width of the Text field relative to the parent. (I imagine I would use grid_columnconfigure(weight=), but am happy to entertain suggestions.)
  3. Word-wrap the Text contents to fit its width.
  4. Vertically expand the Text block to display all lines.
  5. Propagate the vertical size outward to the parent.

这可能有一个直截了当的答案,一旦我更好地理解了 Tkinter 几何管理器的工作原理,特别是网格的工作原理,这个答案就会变得显而易见.我读过 http://effbot.org/tkinterbook/grid.htmhttp://effbot.org/tkinterbook/pack.htm 还没有啊哈!"片刻.如果您提供也能解决此类问题的参考资料,我将不胜感激.

This may have a straightforward answer that would become apparent once I better understand the workings of Tkinter geometry managers in general and Grid in particular. I've read http://effbot.org/tkinterbook/grid.htm and http://effbot.org/tkinterbook/pack.htm without yet having a big "Aha!" moment. I'd be grateful for reference material that addresses this kind of issue as well.

感谢您的帮助!

推荐答案

您正在寻找 消息小部件:

You are looking for the Message widget:

消息小部件是标签的变体,旨在显示多行消息.消息小部件可以环绕文本,并调整其宽度以保持给定的纵横比.

The Message widget is a variant of the Label, designed to display multiline messages. The message widget can wrap text, and adjust its width to maintain a given aspect ratio.

如果您想要多种字体等奇特的东西,您需要移至 Text 小部件.但是,如果您只想要更长版本的 LabelMessage 是一个不错的选择.

If you want fancy things like multiple fonts, you'll need to move to a Text widget. If you just want a longer version of a Label, though, Message is a good choice.

这篇关于带有多行标签的 Tkinter 表单?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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