As the title says, I was wondering if it's possible to change the width of a notebook widget tab?
What I'm trying to do is to make sure that the tabs are of equal size and fill the entire width of the window even when someone resizes it. Currently, it appears like so:
But would like to have it like so:
[I did this one by adding spaces before and after the tab name]
The relevant piece of code for the first one is:
ttk::frame $fr.note.tab1
$fr.note add $fr.note.tab1 -text "Tab1" ;# For the second, it's " Tab1 "
ttk::frame $fr.note.tab2
$fr.note add $fr.note.tab2 -text "Tab2"
ttk::frame $fr.note.tab3
$fr.note add $fr.note.tab3 -text "Tab3"
I tried to look in the manual but it seems that only the widget itself can have its size altered (the area below the tab).
Is there perhaps a way to specify the width of the tab, or a workaround? I tried:
$fr.note add $fr.note.tab1 -text "Tab1" -width 90
But of course, this threw an error saying that the option -width
was an unknown option (and tab doesn't appear to be configurable with no configure
command).
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 ^^
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With