4

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.

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. 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.

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.

Thanks for any help!

Community
  • 1
  • 1
Nat Goodspeed
  • 105
  • 1
  • 1
  • 7
  • Are you simply asking the same thing as this question? [Tkinter Resize text to contents](http://stackoverflow.com/q/11544187/7432) ? – Bryan Oakley Nov 06 '15 at 15:03

1 Answers1

14

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.

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.

TigerhawkT3
  • 48,464
  • 6
  • 60
  • 97
  • 2
    Effbot.org is currently down.Maybe this link can help: https://www.studytonight.com/tkinter/python-tkinter-message-widget. – sputnick567 Apr 10 '21 at 11:33