TIP 496: Display hints in ::entry ::spinbox ::ttk::entry ::ttk::spinbox and ::ttk::combobox

Login
Author:         René Zaumseil <r.zaumseil@freenet.de>
Author:         François Vogel <fvogelnew1@free.fr>
State:          Final
Type:           Project
Vote:           Done
Created:        17-Jan-2018
Post-History:   
Keywords:       Tk
Tcl-Version:    8.7
Tk-Branch:      tip-496

Abstract

Entry fields should support the ability to display informational text when no text is given.

Rationale

Entry fields, for instance search boxes in browsers, have the ability to display some help text inside if no real value is given. The text is with a lighter color and is removed if the user enters some real text.

The entry, spinbox, ttk::entry, ttk::spinbox and ttk::combobox commands can easily be enhanced to provide this support, and there is no interference with existing code as this functionality can be done by just introducing new options.

Specification

A help text can be displayed on entry, spinbox, ttk::entry, ttk::spinbox and ttk::combobox widgets if no text is otherwise displayed. To control the text appearance the following new options will be added:

-placeholder: The string to display. If empty (the default value) then nothing is displayed. This text value is always displayed with current font and justification. Note that justification can really be honored only if the available display space is large enough to contain the complete text value (this is anyway true also for the widget text itself, see Tcl Core discussion).

-placeholderforeground: The color of the text. This is a normal option for the Tk widgets and a style option for the Ttk widgets. Default value is #b3b3b3 (gray70).

Implementation

A patch implementing these changes is available in the fossil repository in the tip-496 branch.

Documentation updates are in the same branch.

Example of use

    package require Tk
    # tk entry
    grid [::ttk::entry .e];#normal display
    .e configure -placeholder test;#display text is: test
    .e configure -placeholderforeground red;#display text is now red
    .e insert end xxx;#display text is xxx, color is black
    .e configure -textvariable a; set a yyy;##display text is yyy
    set a {};#display text is test, color is red
    # ttk entry
    ttk::entry .e1
    ttk::style configure TEntry -placeholderforeground red
    .e1 configure -placeholder test

Copyright

This document has been placed in the public domain.