Is it possible to change width of notebook tab?

前端 未结 2 1148
一个人的身影
一个人的身影 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条回答
  • 2021-01-18 18:04

    Some investigation later, and it seems that you can't due to some bugs in the tab row layout engine.


    It seems that it ought to work like this:

    # An arbitrary value!
    ttk::style configure Wider.TNotebook -mintabwidth 1000
    $fr.note configure -style Wider.TNotebook
    

    This will make the overall space available much less than the space desired, which should cause things to be shared out nicely.

    Except it doesn't. The first tab picks up the increased space, but steals it from the space that the other unexpanded tabs would have taken rather than from the remainder of the space they could have taken. I cannot imagine that this behaviour was intended; it's a bug.

    0 讨论(0)
  • 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 <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 ^^

    0 讨论(0)
提交回复
热议问题