Lostferret
Lostferret

Reputation: 39

Create a Progress bar that updates with a function [Python 3.4]

I have a script that will run for hours at a time, and I have no idea if it's frozen, how long is left, etc. I figured I would try and make a progress bar for it, but I can't seem to wrap my head around creating a progress bar that does not simply increment every X seconds (looking at you tkinter).

The end goal would be to get a very simple progress bar that lets me know my script is still working as it:

1) adds bootstrap support values to a phylogenetic tree using "support_tree = get_support(target_tree,list_of_trees)" from biopython's Phylo. <- this step takes up to 8 hours.

2) starts a new progress bar for when it loops through the nodes on the tree checking for nodes of low support (# of nodes is known, step the for loop is on is known) that increments as it loops through the nodes of the tree.

I'm pretty sure it's just my lack of experience, but I couldn't find a tutorial covering how to hook any type of progress bar into loop functions etc or that wasn't only available for python 2.x Any help is greatly appreciated!

Upvotes: 1

Views: 2531

Answers (1)

en_Knight
en_Knight

Reputation: 5381

A progressbar can be made manually using any GUI package. I like tkinter (or ttk, a typically-included extension package to tk), which has a built-in progressbar class.

here's an example using it How to create downloading progress bar in ttk?

Here's the documentation https://docs.python.org/2/library/ttk.html#progressbar

Here's the New Mexico Tech page on it http://infohost.nmt.edu/tcc/help/pubs/tkinter/web/ttk-Progressbar.html

A simple usage would be

p = ttk.Progressbar(parent, orient=HORIZONTAL, length=200, mode='determinate')
p['maximum'] = 100
for i in range(100):
    time.sleep(1)
    p['value'] = 1

Though this example might not actually draw unless you force it to. A better example is on the linked SO page; this is just simple usage. The "value" property is how much progress has been made, and the "maximum" property is the limiting progress. If you're not used to using tkinter widgets, there are a lot of resources to get you started (the code can be extremely concise if you want it to)

Upvotes: 2

Related Questions