| Ticket UUID: | 596ca7f53067287b3d1b8c7ffe78dbb7a38f252b | |||
| Title: | label text constraint by -width/-height paints into -padx/pady and border space | |||
| Type: | Bug | Version: | 8.6, 9.0 | |
| Submitter: | oehhar | Created on: | 2026-04-17 14:04:28 | |
| Subsystem: | 47. Win Fonts | Assigned To: | nobody | |
| Priority: | 5 Medium | Severity: | Minor | |
| Status: | Open | Last Modified: | 2026-04-20 15:54:07 | |
| Resolution: | None | Closed By: | nobody | |
| Closed on: | ||||
| Description: |
Platform: MS-Windows A text of a label widget paints into the -padx/-pady and border region of a label widget, if the text is constraint by -width/-height. This is elaborated in the comments. --- The original ticket text was: For me on MS-Windows, the -width option of a label does not limit to the given number of characters. font create f1 -family Courier set t "0123456789\n1234567890\n2345678901" pack [label .l1 -text $t -width 3 -height 1 -font f1 -bg yellow] Should show 3 characters and 1 line. For me, it shows:
Is it me? Thanks for all, Harald | |||
| User Comments: |
oehhar added on 2026-04-20 15:54:07:
Yes, correctly. Feature is ok. Nevertheless, I leave the bug open if others are wondering themselfes. Thanks for all, Harald chw added on 2026-04-20 14:55:31: Harald wrote: > So, the only bug is: > > A clipped text is painted into the -padx/-pady and border region > of a label widget. Maybe. OTOH this is the Tk standard most likely since sliced bread was invented or even when gumboots where made of wood. Let's call it a feature and declare victory :) Otherwise, we need the platform and build time specific TkUnixSetXftClipRegion() at even more places. Or triple buffering, or quadruple buffering and so on. oehhar added on 2026-04-20 14:13:57: Ok, that adds salt to the mystery: font pains in -padx and border% font create f1 -family "Courier New" % font measure f1 123 30 % set t "012345678\n123456789\n234567890" % pack [label .l1 -text $t -width 3 -height 1 -font f1 -bg yellow] % winfo reqwidth .l1 36 % pack [label .l2 -text $t -width 3 -height 1 -font f1 -bg red -borderwidth 0 -padx 0] % winfo reqwidth .l2 30 What is observable:
As a sum-up, there is a text clipping issue, which clips into the -padx and -border place. font pains in -pady and borderLets try the same with -pady by using a two line text: % font create f1 -family "Courier New" % set t "0123456789\n1234567890" % pack [label .l1 -text $t -height 1 -font f1 -bg yellow] % winfo reqheight .l1 24 % pack [label .l2 -text $t -height 1 -font f1 -bg red -borderwidth 0 -pady 0] % winfo reqheight .l2 18 Again, the text is painted in the border and -pady region. Courier fontNow, lets look to the Courier font: % font create f1 -family "Courier" % font measure f1 123 27 % set t "012345678\n123456789\n123456789" % pack [label .l1 -text $t -width 3 -height 1 -font f1 -bg yellow] % winfo reqwidth .l1 33 % pack [label .l2 -text $t -width 3 -height 1 -font f1 -bg red -borderwidth 0 -padx 0] % winfo reqwidth .l2 27 The observations are identical to the first example. Only the measured text is 3 pixel shorter. This is consistent with the displayed text. So, the only bug is: A clipped text is painted into the -padx/-pady and border region of a label widget. THanks for all, Harald emiliano added on 2026-04-20 13:25:00: @Harald: it not only makes sense for monospaced fonts. An use case is when you want a series of labels of the same width; in such cases specifying a wide enough width makes all labels require the same space, even if the text is wider than the configured width. Another one is controlling the effect of geometry propagation. If a label is used in a status bar, and you display a long line on it, the geometry propagation can make the whole window wider trying to display all the text. @chw: on Tk labels you have the padding, the borderwidth and the highlightthickness as space taking elements. By default, the highlightthickness is 0, but the others are 1.
oehhar added on 2026-04-20 10:20:10: Thanks for all ! IMHO, a -width in characters makes only sense for monospace fonts. There, it might be used for character filling or truncation. I personally use it to display big characters in a fix-size space for a weighting application. I use "label" instead of "ttk::label", as the background shows, if the weight is close to the should-weight (red/yellow/green background). This is only possible with "label" and not "ttk::label"... I would see much more use-cases to specify the width in pixels. But this is not the case. With "ttk::label" it works as expected. With "label", there are two issues. My opinion is very irrelevant, if this is a bug or not. But the current behaviour makes the "-width" for the use-case "Character truncation" useless. Thanks for all, Harald chw added on 2026-04-20 09:37:35: Harald, in my irrelevant opinion this ain't no bug at all, since the manpage for "label/-width" sez: Specifies a desired width for the label. If an image or bitmap is being displayed in the label then the value is in screen units (i.e. any of the forms acceptable to Tk_GetPixels); for text it is in characters. If this option is not specified, the label's desired width is computed from the size of the image or bitmap or text being displayed in it. No word about what happens if the text is larger. No word about truncating the text on a per character basis. Now do a "font measure f1 [string range $t 0 2]" and see that this gives a number which is very near to "[winfo width .l1]". On X11 with 8.6 I get 32 pixels window width when I set the X padding to zero. The font measurement yields 30 pixels, so the difference is 2 (why I don't know). OK, one might argue, that there's a bug in that the padding (if not zero) is not honored since there's no clipping on the rendered text rendering, i.e. it uses the padding space, if too wide. But then again, if the label's anchor is "center", and the number of chars in the second line is 10, which exact three chars would you expect to be rendered when 10/3 yields not exactly an integer number? oehhar added on 2026-04-20 09:22:13: Lets try with the "ttk::label" widget: font create f1 -family Courier set t "0123456789\n1234567890\n2345678901" pack [ttk::label .l1 -text $t -width 3 -font f1] -> shows exactly 3 characters width on 8.6 and 9.0. So, there is no bug with ttk::label widget, even not when fonts are replaced. Harald oehhar added on 2026-04-20 08:37:08: Nevertheless, on a fixed width font, it should be accurate, no? Yes, the given bcg has an influence and explains the small difference between Tk8.6 and Tk9.0: In tk 8.6:
% font create f1 -family Courier
% font actual f1
-family {Courier New} -size 12 -weight normal -slant roman -underline 0 -overstrike 0
So, "Courier" gets magically changed to "Courier New" when configuring. In Tk 9.0: % font create f1 -family Courier % font actual f1 -family Courier -size 12 -weight normal -slant roman -underline 0 -overstrike 0 So, no magic replacement of "Courier" by "Courier New". The font family "Courier" does not exist on my system:
% expr {"Courier" in [font names]}
0
So, 8.6 explicitly replaces while 9.0 does this only when rendering. So, try the example with "Courier New" on 9.0: font create f1 -family "Courier New" set t "0123456789\n1234567890\n2345678901" pack [label .l1 -text $t -width 3 -height 1 -font f1 -bg yellow] Then, we get the same result of exactly 4 displayed characters. So, we have two bugs:
Platform:
(I first thought, the difference is due to a 32/64 bit or compiler issue) Note that the font man page states, that the family "Courier" always exists. Thus, "Courier" is IMHO a bit the default to choose a monospace font. So, Courier should work well. Thanks for all, Harald emiliano added on 2026-04-17 23:56:01: Well, it never was "accurate". On all platforms, when -width is used for a text only label the actual width is calculated from some average char width, plus some platform specific padding. On both X11 and Mac the "average width" is calculated as
meaning the width of the "0" character, while on Windows you have this:
meaning the real average of the uppercase and lowercase english letters, rounded to the nearest integer. The difference you are observing between 8.6 and 9 might be due to the changes introduced to fix this bug. | |||
Home
Timeline
Branches
Tags
Forum
Tickets
Download
Wiki