2025, Dec 30 05:00
Change tqdm progress bar color dynamically in Python using the colour attribute (no set_color needed)
Learn how to change tqdm progress bar color on the fly in Python. Update it dynamically by setting the colour attribute and use set_description for text.
Changing the color of a tqdm progress bar on the fly sounds like a small tweak, but it’s a common itch when you want more visual feedback than a static bar. The usual API exposes set_description() for text, and it’s natural to look for a similar method for color. There isn’t one. Yet dynamic color updates are possible.
Reproducing the intent
The idea is straightforward: iterate over work items, and for each step, switch the bar’s color. An intuitive first attempt might look like this, trying to call a non-existent method to set the color:
from tqdm import tqdm
import time
palette = ["red", "yellow", "green", "cyan", "blue"]
meter = tqdm(range(5))
for step in meter:
meter.set_color(palette[step])
time.sleep(0.5)
What’s actually going on
tqdm provides a clean way to change the description via set_description(), but it does not offer a similar public method for color switching. However, the progress bar instance carries a colour attribute that can be reassigned as the loop runs. That’s all you need for dynamic styling.
The fix
Manually update the colour attribute in each iteration. This keeps the loop simple and works as you’d expect:
from tqdm import tqdm
import time
hues = iter(["red", "yellow", "green", "cyan", "blue"])
bar = tqdm(range(5))
for _ in bar:
bar.colour = next(hues)
time.sleep(0.5)
Why this matters
When you know that colour is just a settable attribute, you stop searching for a non-existent API and avoid unnecessary workarounds. It’s a small detail that saves time and keeps the code clean while giving you the dynamic visuals you want.
Takeaway
If you want to change tqdm’s appearance during execution, stick to the tools that exist: set_description() for text and the colour attribute for the bar color. Assign a new value to colour inside the loop, and the progress bar will reflect the update immediately.