MiFischer22
MiFischer22

Reputation: 43

Create Status Window in Python

I have a Python script that runs in the background, i.e. when running this script the end user cannot see how far the script is until it is finished after 30 min or if it is stuck somewhere.

I would now like to have a status window for the end user in Windows that displays a text after milestones in the script have been reached (e.g. when the Python interpreter has reached line 100 in the script, the text "10% of processing is complete" is displayed in the status window).

How can I do implement this?

I tried already the tkinter package. Unfortunately, the pop-up window that is opened with the tkinter package always requires an action of the end user, i.e. either press "ok" in the pop-up window or close the pop-up window, until the Python interpreter continues with the execution of the next line of code. However, I want to avoid any action of the end user, but just show some text in the status window, while the Python interpreter continues inimpeded.

Upvotes: 1

Views: 420

Answers (1)

Jason Chia
Jason Chia

Reputation: 1145

Here is a simple function that does the progressbar using the tqdm library.

from tqdm.tk import tqdm
from time import sleep

window = Tk()    

pbar = tqdm(total=30, tk_parent=window) 
# Here you are defining 30 steps in your progress bar. You can adjust this to however many milestones you need.

def run_task():
    for _ in range(30): #dummy job
        sleep(0.1)
        pbar.update(1) #we update the progress bar by 1. 

run_task()
pbar.close()
#If you don't want the button. Do not create it. 
window.destroy()
#Destroy all tkinter windows so nothing is left.

In the above example the progress bar (pbar) is essentially used as a global. You can then update the pbar by calling pbar.update(x) for each step. Without a base code on how you are implementing your task I can't exactly assist anymore than this. The question thread posted suggests threading etc. All of which are good solutions depending on your actual tasking.

Upvotes: 0

Related Questions