Search code examples
pythonprogress-barttk

How to link a Progressbar between a python program and a tkinter window?


I have two .py files in a folder (Main_program.py and HMI.py). The first is a code that contains a large loop (which increments) with a print at the beginning, which displays the evolution of the code execution(10%,20%etc). And the second file is an interface, which contains a button that executes Main_program.py . I would like to create a Progressbar in my interface that would be linked to the evolution of the print in the first code. But how do we do that? thank you very much.

HMI.py :

import tkinter
from tkinter import *
from tkinter import ttk
from Main_program import run_progessbar
...
root = Tk()
...
jj=0
progessBar = ttk.Progressbar(root, orient="horizontal",length=170,
                             style='black.Horizontal.TProgressbar',
                             mode='determinate', variable=jj)
progessBar.place(x=1060,y=180
...

Main_program.py :

def run_progessbar():
    import numpy as np
    import matplotlib
    ...
    global jj  #without function here jj=0
    while ii > 0 and ii <= np.floor(count / Nbtot):
        if np.remainder(ii,Ni / (10*Nbtot)) == 0:
            jj=jj + 10
            print(str(jj)+'%')
        ...
        ii=ii+1

   #in the shell
    global jj
    SyntaxError: name 'jj' is used prior to global declaration

Solution

  • Moab,

    Have you tried something like this already?

    jj = 0
    self.progessBar = ttk.Progressbar(self.frame, orient="horizontal", 
                                      length=286, mode="determinate", 
                                      value=jj).pack(pady=10)
    
    def run_progessbar(self):
    
        global jj
        while ii > 0 and ii <= np.floor(count / Nbtot):
        ...
            jj=jj + 10
            print(str(jj)+'%')
        ...
            ii=ii + 1
    

    I found two useful links:

    Tkinter progress bar updating

    Progress bar updating