Tk Source Code

View Ticket
Login
Ticket UUID: ea0e699ea1aeb8272bfa761992665d5cd13e2ce1
Title: ttk::treeview with stretchable columns requests more width after every style change
Type: Bug Version: 8.6.18, 9.0.5, 9.1.1
Submitter: serhiy.storchaka Created on: 2026-11-23 22:08:40
Subsystem: 88. Themed Tk Assigned To: nobody
Priority: 5 Medium Severity: Minor
Status: Open Last Modified: 2026-11-28 06:46:18
Resolution: None Closed By: nobody
    Closed on:
Description:

A ttk::treeview with a stretchable column, given extra space by its geometry manager, gets wider every time its style is configured, even with the same values, and its neighbors get narrower:

wm geometry . 600x200
ttk::treeview .t -show tree
ttk::frame .f
grid .t .f -sticky nsew
grid columnconfigure . {0 1} -weight 1
grid rowconfigure . 0 -weight 1
.t insert {} end -text Item
update
for {set i 0} {$i < 4} {incr i} {
    puts "[winfo reqwidth .t] [winfo width .f]"
    ttk::style configure Treeview -rowheight 20
    update
}

The requested width of the treeview grows 202, 400, 499, 549, and the width of the frame shrinks 200, 101, 51, 26. The same happens with 8.6.18, 9.0.5 and 9.1.1.

TreeviewSize() requests the sum of the current column widths (TreeWidth()). When the treeview gets more space than it requested, ResizeColumns() stretches column #0 to fill it. So when the style change recomputes the requested size, the treeview requests its stretched width, and the geometry manager distributes the remaining space again. In [62a760c20c] bohagan described the expected behavior: with stretchable columns, the widget width should increase only if the minimum sizes of the stretchable columns and the widths of the other columns exceed the current width.

A workaround is -stretch 0 for the column. Reported in CPython: https://github.com/python/cpython/issues/159078. Related: [b094cb4aa9] and [bc602049ab], where the treeview grew for other reasons.

User Comments: bohagan added on 2026-11-28 06:46:18:

So examining the code I see:

Treeview is registered via:

RegisterWidget(interp, "ttk::treeview", &TreeviewWidgetSpec);

Where TreeviewWidgetSpec defines TreeviewDoLayout as the do layout function. It calls:

Ttk_PlaceLayout(tv->core.layout,tv->core.state,Ttk_WinBox(tv->core.tkwin));

Where Ttk_WinBox gets the widget x, y, width, height, etc. as allocated by the geometry manager.

My analysis:

  • Looking at the dump in treeview_log.txt (see attachments), you can see where we do the 'ttk::style configure' on line 859.
  • On line 981, you can see it properly calculates the needed sizes. Line 982 is the Tk_Window config values from Ttk_WinBox.
  • This is then propagated to the display on line 1073.
  • The geometry manager then updates the Tk window config, which triggers another call to TreeviewDoLayout on line 1278.
  • Line 1279 is where Ttk_WinBox gets the new window config which now includes the additional space.
  • Which is then allocated as extra to column #0 starting on line 1358.
  • With a final display update starting on line 1370.

So I agree this isn't what we would expect, but it is how it works.


bohagan added on 2026-11-28 00:07:51:

Yes, it will continue to grow as long as the grid geometry manager says there is space it can use. Since the window size is 600 pixels wide and the frame uses 200, it will grow until it uses the remaining 400 pixels. Column #0 will get all of it since it's the only column and you told it to expand to fill the available space.


serhiy.storchaka added on 2026-11-27 22:12:57:

Grid gives the treeview a share of the extra space, which is fine, but then the treeview requests that share as its own size: after each style change its requested width becomes its previous actual width. With -width 200 on the frame it still grows (requested width 202, 301, 350, 375; frame width 299, 250, 225, 213), and with a listbox instead of the frame the treeview grows too (202, 319, 377, 406). The requested width should not depend on how much the column was stretched.


bohagan added on 2026-11-27 21:34:41:

This doesn't appear to be a Treeview issue, but instead an artifact of the grid geometry manager and how it allocates space since both widgets have the same weight. Since the frame doesn't have a width (or requested width), it gets whatever is leftover from Treeview. Each time a change is made to Treeview for a widget or style option with the GEOMETRY_CHANGED flag (which is most), it calls Ttk_PlaceLayout which in turn asks grid for space. Since the frame doesn't have a size, grid reallocates its space to give 50% (due to same weights) to the Treeview widget. You can prevent this by adding "-width 200" to the frame creation. Interestingly, if you change the frame to a listbox or other widget and give it a size, then the reverse happens and Treeview shrinks.


bohagan added on 2026-11-24 20:36:58:
I'll look into this one too.

Attachments: