Using Python Progress Bar Libraries: Progress, tqdm, alive‑progress, and PySimpleGUI
This article introduces four popular Python progress‑bar libraries—Progress, tqdm, alive‑progress, and PySimpleGUI—explaining their installation, basic usage, code examples, and visual output so developers can easily add lightweight command‑line or graphical progress indicators to their scripts.
If you have never used a progress bar, you might think it adds unnecessary complexity, but adding one only requires a few lines of code. Below we show how to add progress bars in command‑line scripts and in a PySimpleGUI UI.
Progress
The first library demonstrated is progress . You define the number of iterations, choose a bar type, and update the bar on each iteration.
<code>import time
from progress.bar import IncrementalBar
mylist = [1,2,3,4,5,6,7,8]
bar = IncrementalBar('Countdown', max=len(mylist))
for item in mylist:
bar.next()
time.sleep(1)
bar.finish()
</code>The resulting bar looks like the animated GIF shown below.
Progress also supports several bar styles, which can be selected from its documentation.
tqdm
The tqdm library provides a similar interface with a few extra options.
<code>import time
from tqdm import tqdm
mylist = [1,2,3,4,5,6,7,8]
for i in tqdm(mylist):
time.sleep(1)
</code>The progress bar produced by tqdm is displayed in the following GIF.
More options are documented on the tqdm website.
alive‑progress
As the name suggests, alive_progress adds animated, lively effects to the bar.
<code>from alive_progress import alive_bar
import time
mylist = [1,2,3,4,5,6,7,8]
with alive_bar(len(mylist)) as bar:
for i in mylist:
bar()
time.sleep(1)
</code>The animated result is shown below.
More details and features can be found on its GitHub page.
PySimpleGUI
For a graphical progress bar, PySimpleGUI provides one_line_progress_meter for simple UI feedback.
<code>import PySimpleGUI as sg
import time
mylist = [1,2,3,4,5,6,7,8]
for i, item in enumerate(mylist):
sg.one_line_progress_meter('This is my progress meter!', i+1, len(mylist), '-key-')
time.sleep(1)
</code>A more complex PySimpleGUI example creates a window with a ProgressBar element and updates it inside a loop.
<code>import PySimpleGUI as sg
import time
mylist = [1,2,3,4,5,6,7,8]
progressbar = [[sg.ProgressBar(len(mylist), orientation='h', size=(51,10), key='progressbar')]]
outputwin = [[sg.Output(size=(78,20))]]
layout = [[sg.Frame('Progress', layout=progressbar)], [sg.Frame('Output', layout=outputwin)], [sg.Submit('Start'), sg.Cancel()]]
window = sg.Window('Custom Progress Meter', layout)
progress_bar = window['progressbar']
while True:
event, values = window.read(timeout=10)
if event == 'Cancel' or event is None:
break
elif event == 'Start':
for i, item in enumerate(mylist):
print(item)
time.sleep(1)
progress_bar.UpdateBar(i+1)
window.close()
</code>These examples demonstrate that adding a progress bar to a Python script is straightforward and greatly improves user feedback during long‑running operations.
Python Programming Learning Circle
A global community of Chinese Python developers offering technical articles, columns, original video tutorials, and problem sets. Topics include web full‑stack development, web scraping, data analysis, natural language processing, image processing, machine learning, automated testing, DevOps automation, and big data.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.