Today after some snooping about the source in the folder lib/tk8.6/ttk in the file classicTheme, I found that one can use this to resize the notebook tabs:
ttk::style configure TNotebook.Tab -width 20
Example:
package require Tk ;# 8.6.4
ttk::notebook .note
set note .note
ttk::frame $note.tab1
$note add $note.tab1 -text "Tab 1"
ttk::frame $note.tab2
$note add $note.tab2 -text "Tab 2"
ttk::frame .note.tab3
$note add $note.tab3 -text "Tab 3"
pack $note -fill both -expand 1
set nw [winfo width .note]
set tc [llength [winfo children .note]]
ttk::style configure TNotebook.Tab -width [expr {$nw/6/$tc}]
ttk::style configure TNotebook.Tab -anchor center ;# To center tab label
The above gives:
All the notes will inherit the same configurations however, but in case a window is too small, the tabs will evenly resize:
You might notice I divided by 6 when setting the width of the tab, that was the factor difference I found between the value given by winfo width
and -width
when configuring the tab (directly using $nw/$tc
would otherwise give huge tab widths).
As an extra, one can also bind the note to <Configure>
to allow the tabs to automatically resize to fit the window if the window size changes:
bind .note <Configure> {
set nw [winfo width .note]
set tc [llength [winfo children .note]]
ttk::style configure TNotebook.Tab -width [expr {$nw/6/$tc}]
}
I'm not entirely sure whether this could be considered good practice or not, it works for me ^^