Step 4: Choose your own widgets¶
The bar’s built-in display won’t always be what you want on screen. This step replaces it entirely with a hand-picked, explicit list of widgets.
"""Replace the default widget set with your own list.
`widgets=[...]` overrides everything `ProgressBar` would otherwise have
picked based on `max_value` -- the previous step's percentage, bar and ETA
are gone unless you list them again yourself.
"""
import time
import progressbar
def main() -> None:
widgets = [
progressbar.Percentage(),
' ',
progressbar.Bar(),
' ',
progressbar.ETA(),
]
with progressbar.ProgressBar(max_value=100, widgets=widgets) as bar:
for i in range(100):
time.sleep(0.01)
bar.update(i + 1)
if __name__ == '__main__':
main()
The previous step let ProgressBar pick its own display based on
max_value. Here the constructor gains a widgets=widgets argument,
where widgets is a plain list built before the call:
[progressbar.Percentage(), ' ', progressbar.Bar(), ' ',
progressbar.ETA()]. Passing widgets overrides everything
ProgressBar would otherwise have picked – only what is listed is
shown, in the order listed, including the plain ' ' strings used here
to space the widgets apart.