PercentageLabelBar

PercentageLabelBar centers the percentage inside a fill bar.

Reach for it for a compact bar that carries its own percentage label rather than printing it beside the bar as a separate widget. A specialization of FormatLabelBar with the format fixed to the percentage.

class progressbar.widgets.PercentageLabelBar(format='%(percentage)2d%%', na='N/A%%', **kwargs: Any)[source]

Bases: Percentage, FormatLabelBar

A bar which displays the current percentage in the center.

Create a PercentageLabelBar.

Parameters:
  • format – The percentage template (see Percentage). Uses %2d rather than Percentage’s default %3d, which adds a padding space that looks off-centre here.

  • na – Template used when no percentage is available.

  • **kwargs – Forwarded to FormatLabelBar.__init__.

Example

PercentageLabelBar
"""``PercentageLabelBar`` centers the percentage inside a fill bar.

Reach for it for a compact bar that carries its own percentage label
rather than printing it beside the bar as a separate widget. A
specialization of ``FormatLabelBar`` with the format fixed to the
percentage.
"""

import time

import progressbar

STEPS = 24


def main() -> None:
    widgets = [progressbar.PercentageLabelBar()]
    with progressbar.ProgressBar(max_value=STEPS, widgets=widgets) as bar:
        for step in range(STEPS):
            bar.update(step + 1)
            time.sleep(0.005)


if __name__ == '__main__':
    main()

See also

  • FormatLabelBar: the general form this fixes to the percentage.

  • Percentage: the plain percentage readout this overlays on a bar.