Is it possible to change width of notebook tab?

前端 未结 2 1174
一个人的身影
一个人的身影 2021-01-18 17:44

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

2条回答
  •  梦毁少年i
    2021-01-18 18:04

    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:

    enter image description here

    All the notes will inherit the same configurations however, but in case a window is too small, the tabs will evenly resize:

    enter image description here

    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 to allow the tabs to automatically resize to fit the window if the window size changes:

    bind .note  {
      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 ^^

提交回复
热议问题