TIP 577: Enhanced index values for Tk

Author:         Jan Nijtmans <[email protected]>
State:          Final
Type:           Project
Vote:           Done
Created:        8-June-2020
Keywords:       Tcl index
Tcl-Version:    8.7
Tk-Branch:      enhanced-index
Vote-Summary    Accepted 5/0/1
Votes-For:      BG, JN, KW, MC, SL 
Votes-Against:  none
Votes-Present:  KBK


Various commands handle errors in "index" values differently. This TIP attempts to treat the different uses of index values in Tk to a more uniform way, align them better with index usage in Tcl.

The main feature of this TIP is to let Tk indices handle all forms of Tcl indices. So "4+5" and "end-1" will now be accepted, just as any other form accepted by Tcl_GetIntFormIndex() (See also TIP #544).

In addition, the empty string is accepted in Tk as valid index. It means the same as -1 so not available. In Tk this is more natural, since the default of many options is already the empty string. The default value of the -underline option becomes the empty string. Setting -underline to the empty string means no underlining. This default value change will be only done when Tk is compiled with Tcl 9 headers, in order to give applications more time to adapt to this usage (see Compatibility section below).


Some examples (Tk):

$ wish8.6
% listbox .l
% menu .m
% .l index 0
% .l index none (listbox doesn't know about none)
bad listbox index "none": must be active, anchor, end, @x,y, or a number
% .m index none (menu does know about none)
% .m index 0

We see that menu's have a different way to indicate not available compared to other widgets.

A request is done already a long time ago, to make index handling in Tk more flexible. See: widgets indices enhancement This TIP implements part of this request (only the active-<integer> form is not implemented.



When compiled with Tcl 8.x headers, this is almost 100% compatible. Except for menu commands which currently use the inconsistent none keyword (see above example) they will start using the empty string in stead (but none still works in Tcl 8.x).

Code can easily be made working in both Tcl 8.x and 9.x environments. Just don't check against the value -1 any more, but use the < 0 check: Since comparing the empty string with 0 in Tcl uses string compare, it gives the same result as comparing -1 with 0. All Tk utilities and demo's are already modified to do this check consistently.


See the enhanced-index branch.


This document has been placed in the public domain.