Search code examples
pythonprogress-barcurses

Progress bar in python curses


I have created a progress bar which updates itself after getting a percentage from another function but I'm having issues getting it to trail like this ############. Instead, it just move the '#' to the right until 100% is reached. Below is my code. The reason why it's this way is because I need the percentage to come externally so that the code can be reusable. please help me.

import curses
import time

curses.initscr()

def percentage():
    loading = 0
    while loading < 100:
        loading += 1
        time.sleep(0.03)
        update_progress(loading)


def update_progress(progress):
    win = curses.newwin(3, 32, 3, 30)
    win.border(0)
    rangex = (30 / float(100)) * progress
    pos = int(rangex)
    display = '#'
    if pos != 0:
        win.addstr(1, pos, "{}".format(display))
        win.refresh()

percentage()

Solution

  • you can just switch the pos to multiply the display #:

    if pos != 0:
        win.addstr(1, 1, "{}".format(display*pos))
        win.refresh()