TIP 442: Display text in progressbars

Login
Bounty program for improvements to Tcl and certain Tcl packages.
Author:         René Zaumseil <rzaumseil@kkg.ch>
Author:         Kevin B Kenny <kevin.b.kenny@gmail.com>
Author:         Andreas Leitgeb <avl@logic.at>
Author:         Kevin Kenny <Kevin.B.Kenny@gmail.com>
State:          Final
Type:           Project
Vote:           Done
Created:        17-Feb-2016
Post-History:   
Keywords:       Tk
Tcl-Version:    8.7

Abstract

Horizontal progress bars should support the ability to display text inside the progress bar. Buttons should allow justification of multiline texts.

Rationale

It is often useful to be able to display text directly on top of a progress bar. This text might be a description of the progress percentage, what is currently being done, or even just a label giving the overall task that is progressing.

The ttk::progressbar command can easily enhanced to provide this support, and there is no interference with existing code as this functionality can be done by just introducing new options. The options required are from the list of usual Tk well-known option names.

Also the ttk::button command can easily be enhanced to provide justification of multiline text.

Specification

Text will be displayed only on horizontal ttk::progressbar. To control the text appearance the following new options will be added:

-text: The string to display.

-font: The font used to render the text.

-foreground: The color of the text.

-anchor: The anchoring of the text.

-justify: The justification of the string.

-wraplength: The length at which the string will be automatically wrapped.

To justify multiline text in ttk::button a new option will be added:

-justify: The justification of the string.

Notes for future improvements

The underlying Tk text rendering engine supports rotated text, which would make support on vertical progress bars possible. But control of the rotation angle might be required (according to whether the text is rotated left or right, or stays unrotated).

The most contrasting color of the text will depend where on the progress bar it is placed. This is not an effect that is simply reproduced with the Tk script level, but is easy to apply during rendering.

Implementation

A patch implementing these changes and updating the documentation is available in the fossil repository in the tip-442 branch.

Implementation is heavily borrowed from the ttk::label widget featuring these same options. The names, meanings, and default values of the options are the same as for ttk::label. The rendering and processing is the same as for this latter widget.

Example of use

    package require Tk
    proc moveit {} {
      for {set i 0} {$i < 100} {incr i} {
        .p step ; update ; after 100
      }
    }
    pack [ttk::progressbar .p -value 0 -maximum 50 -orient horizontal -length 500]
    .p configure -anchor c -foreground blue -justify right \
            -text "-anchor c -foreground blue -justify right -wraplength 100" \
            -wraplength 100
    moveit
    .p configure -anchor e -font {Arial 10 bold} -foreground green -justify center \
            -text "-anchor e -font {Arial 10 bold} -foreground green -justify center -wraplength 250" \
            -wraplength 250
    moveit
    .p configure -text "-anchor w -foreground red -justify left -wraplength 50" \
            -anchor w -foreground red -justify left -wraplength 50
    moveit
    .p configure -orient vertical -text "Cannot be seen"
    moveit

Copyright

This document has been placed in the public domain.

History