Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Tkinter form with multi-line labels?

Tags:

python

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.

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!

like image 581
Nat Goodspeed Avatar asked Dec 10 '22 20:12

Nat Goodspeed


1 Answers

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.

like image 196
TigerhawkT3 Avatar answered Dec 30 '22 16:12

TigerhawkT3