I have a ttk::treeview where some of the labels are longish. The
treeview has both horizontal and vertical scrollbars, but the
horizontal scrollbar never seems to be "useful", i.e. its slider
always has maximum width, the arrows at either end are always grayed-
out and non-functional and if the mouse is over it, the scrollwheel
is non-functional. The script below illustrates the problem:
package require tk wm withdraw .
set tl [toplevel .tl] set frm [ttk::frame $tl.frm]
set sub [ttk::frame $frm.sub]
set tv [ttk::treeview
$sub.tv \ -columns
One \ -displaycolumns
{} \ -show tree] set vsb [ttk::scrollbar $sub.vsb \ -
orient vertical \ -command
[list $tv yview]] set hsb [ttk::scrollbar
$sub.hsb \ -orient
horizontal \ -command [list
$tv xview]] $tv
configure \ - yscrollcommand [list $vsb set] \ - xscrollcommand [list $hsb set]
set displayFormat "%H:%M:%S %A %e-%b-%Y" set dummy [$tv insert {}
end -text [clock format [clock seconds] \ -format
$displayFormat]]
grid $tv -row 0 -column 0 -sticky nsew grid $vsb -row 0 -column 1 -
sticky nsew grid $hsb -row 1 -column 0 -sticky nsew
grid rowconfigure $sub 0 -weight 1 grid columnconfigure $sub 0 -
weight 1
set year "" set month "" foreach date {1719347856 1749064696} { set
newYear [clock format $date -format %Y] if {$newYear ne $year} { set
yearItm [$tv insert {} end -text $newYear] set year $newYear set
month "" }
set newMonth [clock format $date -format %B] if {$newMonth ne
$month} { set monthItm [$tv insert $yearItm end -text $newMonth] set
month $newMonth }
$tv insert $monthItm end -text \
[clock format $date -format $displayFormat] }
set buttons [ttk::frame $frm.btn] set button [ttk::button
$buttons.btn -text Dismiss -command exit]
pack $button -padx 5 -pady 5 -side right
pack $sub -expand yes -fill both pack $buttons -expand yes -fill x
pack $frm -expand yes -fill both grid $frm -row 0 -column 0 -sticky
nsew
grid rowconfigure $tl 0 -weight 1 grid columnconfigure $tl 0 -weight
1
focus $button
I'd hoped that the scrollbar would become "useful" once a month item
was opened. I suspect that I may be doing nothing wrong because if I
do away with the hierarchical treeview structure by rewriting the
foreach loop as
foreach date {1719347856 1749064696} { $tv insert {} end -text
[clock format $date -format $displayFormat] }
the horizontal scrollbar is still non-functional.
I'm also spooked by the note from ofv about a quarter the way down
the page at https://wiki.tcl-lang.org/page/ttk%3A%3Atreeview, which describes a somewhat similar fault.
Alan
The problem is you are mixing grid and pack, in addition to not
specifying a column width. Treeview will by default auto resize columns
if not told what size to use, therefore the scrollbar won't do anything.
So you need to use the -minwidth and -width values. Also, the tree
column is always column #0, you can't rename it, so the -columns option isn't needed. Here's a cleaned up version:
package require Tk
wm withdraw .
set tl [toplevel .tl]
# Tree frame
set tf [ttk::frame $tl.tf]
set tv [ttk::treeview $tf.tv -show tree]
$tv column #0 -minwidth 250 -width 300
set vsb [ttk::scrollbar $tf.vsb -orient vertical \
-a-a-a-a-command [list $tv yview]]
set hsb [ttk::scrollbar $tf.hsb -orient horizontal \
-a-a-a-a-command [list $tv xview]]
$tv configure -yscrollcommand [list $vsb set] \
-a-a-a-a-xscrollcommand [list $hsb set]
set displayFormat "%H:%M:%S %A %e-%b-%Y"
set dummy [$tv insert {} end -text [clock format [clock seconds] \
-a-a-a -format $displayFormat]]
grid $tv-a -row 0 -column 0 -sticky nsew
grid $vsb -row 0 -column 1 -sticky ns
grid $hsb -row 1 -column 0 -sticky ew
grid rowconfigure $tf 0 -weight 1
grid columnconfigure $tf 0 -weight 1
# Treedata
set year ""
set month ""
foreach date {1719347856 1749064696} {
-a-a-a set newYear [clock format $date -format %Y]
-a-a-a if {$newYear ne $year} {
-a-a-a-aset yearItm [$tv insert {} end -text $newYear]
-a-a-a-aset year $newYear
-a-a-a-aset month ""
-a-a-a }
-a-a-a set newMonth [clock format $date -format %B]
-a-a-a if {$newMonth ne $month} {
-a-a-a-aset monthItm [$tv insert $yearItm end -text $newMonth]
-a-a-a-aset month $newMonth
-a-a-a }
-a-a-a $tv insert $monthItm end \
-a-a-a-a-text [clock format $date -format $displayFormat]
}
# Button frame
set bf [ttk::frame $tl.bf]
set button [ttk::button $bf.btn -text Dismiss -command exit]
grid $button -sticky nse
grid columnconfigure $bf 0 -weight 1
# All
grid $tf -sticky nsew
grid $bf -sticky ew -padx 10 -pady 10
grid rowconfigure $tl 0 -weight 1
grid columnconfigure $tl 0 -weight 1
focus $button
On 1/9/26 9:00 AM, Alan Grunwald wrote:
I have a ttk::treeview where some of the labels are longish. The
treeview has both horizontal and vertical scrollbars, but the
horizontal scrollbar never seems to be "useful", i.e. its slider
always has maximum width, the arrows at either end are always grayed-
out and non-functional and if the mouse is over it, the scrollwheel
is non-functional. The script below illustrates the problem:
package require tk wm withdraw .
set tl [toplevel .tl] set frm [ttk::frame $tl.frm]
set sub [ttk::frame $frm.sub]
set tv [ttk::treeview
$sub.tv-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a \ -columns
One-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a \ -displaycolumns
{}-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a \ -show tree] set vsb
[ttk::scrollbar $sub.vsb-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a \ -
orient vertical-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a \ -command
[list $tv yview]] set hsb [ttk::scrollbar
$sub.hsb-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a \ -orient
horizontal-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a \ -command [list
$tv xview]] $tv
configure-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a \ -
yscrollcommand [list $vsb set]-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a \ -
xscrollcommand [list $hsb set]
set displayFormat "%H:%M:%S %A %e-%b-%Y" set dummy [$tv insert {}
end -text [clock format [clock seconds]-a-a-a-a-a \ -format
$displayFormat]]
grid $tv-a -row 0 -column 0 -sticky nsew grid $vsb -row 0 -column 1 -
sticky nsew grid $hsb -row 1 -column 0 -sticky nsew
grid rowconfigure $sub 0 -weight 1 grid columnconfigure $sub 0 -
weight 1
set year "" set month "" foreach date {1719347856 1749064696} { set
newYear [clock format $date -format %Y] if {$newYear ne $year} { set
yearItm [$tv insert {} end -text $newYear] set year $newYear set
month "" }
set newMonth [clock format $date -format %B] if {$newMonth ne
$month} { set monthItm [$tv insert $yearItm end -text $newMonth] set
month $newMonth }
$tv insert $monthItm end -text-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a \
[clock format $date -format $displayFormat] }
set buttons [ttk::frame $frm.btn] set button [ttk::button
$buttons.btn -text Dismiss -command exit]
pack $button -padx 5 -pady 5 -side right
pack $sub -expand yes -fill both pack $buttons -expand yes -fill x
pack $frm -expand yes -fill both grid $frm -row 0 -column 0 -sticky
nsew
grid rowconfigure $tl 0 -weight 1 grid columnconfigure $tl 0 -weight
1
focus $button
I'd hoped that the scrollbar would become "useful" once a month item
was opened. I suspect that I may be doing nothing wrong because if I
do away with the hierarchical treeview structure by rewriting the
foreach loop as
foreach date {1719347856 1749064696} { $tv insert {} end -text
[clock format $date -format $displayFormat] }
the horizontal scrollbar is still non-functional.
I'm also spooked by the note from ofv about a quarter the way down
the page at https://wiki.tcl-lang.org/page/ttk%3A%3Atreeview, which
describes a somewhat similar fault.
Alan
The immediate follow-up question that occurs to me is "What value should
I choose for the -minwidth option?", which leads on to a couple of other questions:
o-a-a-a-a I can keep track of the various strings I want to display,
-a-a-a-a-a determine their sizes using [font measure] and ensure that
-a-a-a-a-a -minwidth and -width are set to their lengths, but what font
-a-a-a-a-a should I specify? I could configure the font on a per-tag basis
-a-a-a-a-a and then find that font via [$tv tag configure {} -font] but that
-a-a-a-a-a returns an empty string in my case, where I haven't configured a
-a-a-a-a-a font. I guess it's TkDefaultFont - it would be nice not to have
-a-a-a-a-a to guess but I can't find anything in the documentation.
o-a-a-a-a How much leading space do I need to allow? In my example above,
-a-a-a-a-a the strings are displayed on items that are down two levels. I
-a-a-a-a-a can find how much each level is indented via
-a-a-a-a-a [ttk::configure [winfo style $tv] -indent]
-a-a-a-a-a but if I add double the indent to the length of the longest
-a-a-a-a-a string, and use the result to set -minwidth, it's not enough. I
-a-a-a-a-a guess the extra space is required by the icon used to indicate
-a-a-a-a-a that the some of the items have children, but I can't find what
-a-a-a-a-a that image is.
Honestly? I think this stuff is hard and complicated enough that it
ought to be done by the widget itself, and the fact that it doesn't
should be regarded at least as a serious shortcoming, if not as a bug.
Alan
Don't over think it. Pick a reasonable width based on the data. The user
can resize the window to make it bigger. Use what you consider the
minimum viewable size for -minwidth, which is where the scrollbar will
take over. How much leading space you should account for depends on how
much nesting you plan to do. Again, the user can resize the widget to
see more.
...
The immediate follow-up question that occurs to me is "What value
should I choose for the -minwidth option?", which leads on to a couple
of other questions:
o-a-a-a-a I can keep track of the various strings I want to display,
-a-a-a-a-a-a determine their sizes using [font measure] and ensure that
-a-a-a-a-a-a -minwidth and -width are set to their lengths, but what font
-a-a-a-a-a-a should I specify? I could configure the font on a per-tag basis >> -a-a-a-a-a-a and then find that font via [$tv tag configure {} -font] but that
-a-a-a-a-a-a returns an empty string in my case, where I haven't configured a
-a-a-a-a-a-a font. I guess it's TkDefaultFont - it would be nice not to have >> -a-a-a-a-a-a to guess but I can't find anything in the documentation.
o-a-a-a-a How much leading space do I need to allow? In my example above,
-a-a-a-a-a-a the strings are displayed on items that are down two levels. I >> -a-a-a-a-a-a can find how much each level is indented via
-a-a-a-a-a-a [ttk::configure [winfo style $tv] -indent]
-a-a-a-a-a-a but if I add double the indent to the length of the longest
-a-a-a-a-a-a string, and use the result to set -minwidth, it's not enough. I >> -a-a-a-a-a-a guess the extra space is required by the icon used to indicate >> -a-a-a-a-a-a that the some of the items have children, but I can't find what >> -a-a-a-a-a-a that image is.
Honestly? I think this stuff is hard and complicated enough that it
ought to be done by the widget itself, and the fact that it doesn't
should be regarded at least as a serious shortcoming, if not as a bug.
Alan
On 10/01/2026 21:22, Brian wrote:
Don't over think it. Pick a reasonable width based on the data. TheSage advice.
user can resize the window to make it bigger. Use what you consider
the minimum viewable size for -minwidth, which is where the scrollbar
will take over. How much leading space you should account for depends
on how much nesting you plan to do. Again, the user can resize the
widget to see more.
...
The immediate follow-up question that occurs to me is "What value
should I choose for the -minwidth option?", which leads on to a
couple of other questions:
o-a-a-a-a I can keep track of the various strings I want to display,
-a-a-a-a-a-a determine their sizes using [font measure] and ensure that
-a-a-a-a-a-a -minwidth and -width are set to their lengths, but what font >>> -a-a-a-a-a-a should I specify? I could configure the font on a per-tag basis
-a-a-a-a-a-a and then find that font via [$tv tag configure {} -font] but that
-a-a-a-a-a-a returns an empty string in my case, where I haven't configured a
-a-a-a-a-a-a font. I guess it's TkDefaultFont - it would be nice not to have
-a-a-a-a-a-a to guess but I can't find anything in the documentation.
o-a-a-a-a How much leading space do I need to allow? In my example above, >>> -a-a-a-a-a-a the strings are displayed on items that are down two levels. I >>> -a-a-a-a-a-a can find how much each level is indented via
-a-a-a-a-a-a [ttk::configure [winfo style $tv] -indent]
-a-a-a-a-a-a but if I add double the indent to the length of the longest >>> -a-a-a-a-a-a string, and use the result to set -minwidth, it's not enough. I
-a-a-a-a-a-a guess the extra space is required by the icon used to indicate >>> -a-a-a-a-a-a that the some of the items have children, but I can't find what
-a-a-a-a-a-a that image is.
Honestly? I think this stuff is hard and complicated enough that it
ought to be done by the widget itself, and the fact that it doesn't
should be regarded at least as a serious shortcoming, if not as a bug.
Alan
Sadly, I'm a good way down the track of trying to address this (and have already thought of an-a alternative way to fix things). This is important
to me because I have treeview widgets with associated scrollbars in
several places and I find that none of them scroll horizontally
correctly and sometimes the user can't resize the treeview. This is
probably because I'm not managing it correctly with grid/pack but I
can't face tracking that down at the moment.
In summary, this is a nice rat hole; I think I'll stay here for a while :-)
Alan
| Sysop: | Amessyroom |
|---|---|
| Location: | Fayetteville, NC |
| Users: | 54 |
| Nodes: | 6 (0 / 6) |
| Uptime: | 07:26:00 |
| Calls: | 743 |
| Files: | 1,218 |
| Messages: | 189,495 |