How to Use Progress Bars in Python with 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 with concise code examples, and visual output, helping developers 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 in reality it only requires a few lines of code. This guide shows how to add progress bars in command‑line scripts and PySimpleGUI UI.
Four commonly used Python progress‑bar libraries are introduced: Progress , tqdm , alive‑progress , and PySimpleGUI .
Progress – Define the number of iterations, bar type, and update the bar each iteration. Example code:
<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 incremental progress bar looks like this:
tqdm – Similar usage with a slightly different API. Example code:
<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 appears as follows:
alive‑progress – Provides animated, more lively progress bars. Example code:
<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>Animated output looks like this:
PySimpleGUI – Enables graphical progress meters in a windowed UI. Example code for a simple one‑line progress meter:
<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>For a full‑featured PySimpleGUI application with a progress bar widget, use the following code:
<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>Running this script displays a graphical window with a progress bar that updates as the loop progresses.
In summary, adding a progress bar to a Python script is straightforward and greatly improves visibility into script execution.
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.