Programming Entry and Spinbox Widgets
The default bindings for entry and spinbox widgets are fairly good. However, you can completely control the entry with a set of widget operations for inserting, deleting, selecting, and scrolling. The operations involve addressing character positions called indices. The indices count from zero. The entry defines some symbolic indices such as end. The index corresponding to an X coordinate is specified with @xcoord, such as @26. Table 34-4 lists the formats for indices.
Table 34-4. Entry and spinbox indices0 | Index of the first character. | anchor | The index of the anchor point of the selection. | end | Index just after the last character. | number | Index a character, counting from zero. | insert | The character right after the insertion cursor. | sel.first | The first character in the selection. | sel.last | The character just after the last character in the selection. | @xcoord | The character under the specified X coordinate. |
Table 34-5 summarizes the operations on entry and spinbox widgets. In the table, $w is an entry or spinbox widget.
Table 34-5. Entry and spinbox operations$w bbox index | Returns a list of 4 numbers describing the bounding box of the character given by index. | $w cget option | Returns the value of the configuration option. | $w configure ... | Queries or modifies the widget configuration. | $w delete first ?last? | Deletes the characters from first to last, not including the character at last. The character at first is deleted if last is not specified. | $w get | Returns the string in the entry. | $w icursor index | Moves the insert cursor. | $w identify x y | Identifies the spinbox element at the given x/y coordinate:
none, buttondown, buttonup, entry. Spinbox only. (Tk 8.4) | $w index index | Returns the numerical index corresponding to index. | $w insert index string | Inserts the string at the given index. | $w invoke element | Invokes the spinbox element, either buttondown or buttonup, triggering the action associated with it. Spinbox only. (Tk 8.4) | $w scan mark x | Starts a scroll operation. x is a screen coordinate. | $w scan dragto x | Scrolls from previous mark position. | $w selection adjust index | Moves the boundary of an existing selection. | $w selection clear | Clears the selection. | $w selection element ?element? | Sets or gets the currently selected spinbox element. Spinbox only. (Tk 8.4) | $w selection from index | Sets the anchor position for the selection. | $w selection present | Returns 1 if there is a selection in the entry. | $w selection range start end | Selects the characters from start to the one just before end. | $w select to index | Extends the selection. | $w set ?value? | Gets or sets the value of the spinbox, triggering validation if it is on. Spinbox only. (Tk 8.4) | $w validate | Force an evaluation of the -validatecommand script, returning 0 or 1. (Tk 8.3) | $w xview | Returns the offset and span of visible contents. These are both real numbers between 0 and 1.0. | $w xview index | Shifts the display so the character at index is at the left edge of the display. | $w xview moveto fraction | Shifts the display so that fraction of the contents are off the left edge of the display. | $w xview scroll num what | Scrolls the contents by the specified number of what, which can be units or pages. |
For example, the binding for <Button-1> includes the following commands:
%W icursor @%x
%W select from @%x
if {[%W cget -state] == "normal"} {focus %W}
Recall that the % triggers substitutions in binding commands, and that %W is replaced with the widget pathname and %x is replaced with the X coordinate of the mouse event. Chapter 29 describes bindings and these substitutions in detail. These commands set the insert point to the point of the mouse click by using the @%x index, which will be turned into something like @17 when the binding is invoked. The binding also starts a selection. If the entry is not in the disabled state, then keyboard focus is given to the entry so that it gets KeyPress events.
|