Tk Source Code

View Ticket
Login
Ticket UUID: d6fd19e4e56b52866227e2d6e41936f9e6002edd
Title: Documentation of ttk::notebook "tabs" widget command clarification
Type: Bug Version: 8.6.4
Submitter: anonymous Created on: 2015-07-04 04:36:38
Subsystem: 88. Themed Tk Assigned To: fvogel
Priority: 5 Medium Severity: Minor
Status: Closed Last Modified: 2017-04-07 20:11:16
Resolution: Fixed Closed By: fvogel
    Closed on: 2017-04-07 20:11:16
Description:
The [ttk::notebook] man page specifies that the widget command (given the notebook is .nb) [.nb tabs] "Returns the list of windows managed by the notebook", is it guaranteed that the list of subwindows returned will be in the index order of their accompanying tab? In other words, is it safe to [lindex [.nb tabs] [.nb index current]] to get the currently selected tab's subwindow?

Either way I propose the documentation be updated to explicitly describe the order of the list returned by [.nb tabs].
User Comments: fvogel added on 2017-04-07 20:11:16:
Merged to core-8-6-branch and trunk.

fvogel added on 2017-04-03 21:29:35:

Documentation updated in branch bug-d6fd19e4e5.

Note that this order is also respected by the ttk::panedwindow:

package require Tk

pack [ttk::panedwindow .p -height 100 -width 500] -expand true -fill both
update

set imax 10
for {set i 0} {$i <= $imax} {incr i} {
    .p add [label .p.f$i -text .p.f$i]
}

# shuffle the panes by moving them
for {set i $imax} {$i < 1000} {incr i} {
    set ind [expr round(rand()*[expr {[llength [.p panes]]-1}])]
    set ind2 [expr round(rand()*[expr {[llength [.p panes]]-1}])]
    .p insert $ind2 .p.f$ind
}
.p panes ; # returns the list of panes in order

# shuffle the panes by forgetting/creating them
for {set i $imax} {$i < 1000} {incr i} {
    set ind [expr round(rand()*[expr {[llength [.p panes]]-1}])]
    .p forget $ind
    incr i
    set ind [expr round(rand()*[expr {[llength [.p panes]]-1}])]
    .p insert $ind [label .p.f$i -text .p.f$i]
}
.p panes ; # returns the list of panes in order

This is not surprising since for both ttk::notebook and ttk::panedwindow tabs/panes are managed by Ttk_InsertSlave(), Ttk_ForgetSlave(), Ttk_ReorderSlave() which all maintain the order of slaves (see ttkManager.c).


fvogel added on 2017-04-03 15:25:09:

Inspection of the source code tends to make me think it's indeed safe.

This can also be demonstrated by the following script:

package require Tk

pack [ttk::notebook .n -height 100 -width 500]

set imax 10
for {set i 0} {$i <= $imax} {incr i} {
    .n add [frame .n.f$i] -text Tab_$i
}
# shuffle the tabs
for {set i 1} {$i < 1000} {incr i} {
    set ind [expr round(rand()*[expr {[.n index end]-1}])]
    .n forget $ind
    incr imax
    set ind [expr round(rand()*[expr {[.n index end]-1}])]
    .n insert $ind [frame .n.f$imax] -text Tab_$imax
}
.n select 5
lindex [.n tabs] [.n index current]  ; #returns .n.f$i
         ; # with i consistent with the selected tab label

I will update the man page accordingly.