0

I have created 2 buttons that import Excel files using tkinter library. I have also created function for each respective button so that when user clicks on the button the function will run. All the buttons are working fine but I wanted to know how to add progress bar to each button so that user can see when file is being imported and codes are executed. I have reviewed other similar posts but is not really helpful.

Here is my code:

from tkinter import *
from tkinter import filedialog
import pandas as pd
import numpy as np
from tkinter import ttk

def open_ec_file():
    global ec_df
    #pandas code goes here

def open_lms_file():
    global lms_df
    #pandas code goes here

window = Tk()
#Set title
window.title ('Report Generator')
# Set window size
window.geometry("800x500")
# add logo
window.iconbitmap('logo.ico')

Button(window, text = 'Import EC employee data', command = open_ec_file, bg = 'red', fg = 'white',
       height = 2, font = 'none 14 bold') .grid(row = 2, sticky = E, padx = 6, pady = 8 )
Button(window, text = 'Import LMS data', command = open_lms_file, bg = 'red', fg = 'white',
       font = 'none 14 bold') .grid(row = 4, column = 5, sticky = E, padx = 6, pady = 8 )

Michael
  • 55
  • 1
  • 11
  • What exactly is the problem you are facing? Adding the progressbar to the dialog, or updating it, or finding out how long the functions are going to be running? – tobias_k Jun 23 '21 at 07:14
  • @tobias_k I am wondering how to add progress bar under buttons. I wanted to add ```HORIZONTAL ``` and ```indeterminate``` type until codes are executed. So when files are imported successfully, user can see 100% on progress bar something like this. – Michael Jun 23 '21 at 09:14
  • Which files are imported? – Delrius Euphoria Jun 23 '21 at 09:27
  • @CoolCloud A raw data in Excel file format from user computer – Michael Jun 23 '21 at 10:55
  • Probably keep it in a thread and then check if the thread is alive, make sure the thread has no GUI stuff – Delrius Euphoria Jun 23 '21 at 11:22

1 Answers1

3

If you have the option to update the progress bar manually at certain points in your code, i would do the following:

from tkinter import *
from tkinter import ttk
import time


def update_progress(value):
    progress_bar["value"] = value
    window.update()


def your_time_consuming_function():
    print("Starting function")
    update_progress(0)
    time.sleep(2)
    print("I reached my first checkpoint")
    update_progress(25)
    time.sleep(2)
    print("Another checkpoint, yay")
    update_progress(50)
    time.sleep(2)
    print("Only 1 more to go")
    update_progress(75)
    time.sleep(2)
    print("Yay im finished")
    update_progress(100)


window = Tk()
#Set title
window.title ('Report Generator')
# Set window size
window.geometry("800x500")
# add logo
# window.iconbitmap('logo.ico')

progress_bar = ttk.Progressbar(window, length=100, mode="determinate", orient="horizontal")
progress_bar.grid(row=99, column=0, sticky=E, padx=2, pady=2)

Button(window, text='Test progressbar', command=your_time_consuming_function, bg='red', fg='white',
       height=2, font='none 14 bold').grid(row=2, sticky=E, padx=6, pady=8 )

window.mainloop()

You can use the same progressbar for any function by calling update_progress(value) then.

mnikley
  • 1,625
  • 1
  • 8
  • 21
  • Is it possible to make the ```Progressbar``` mode ```"indeterminate"```? – Michael Jun 23 '21 at 12:42
  • Im not exactly sure what your goal is, but check out [this post](https://stackoverflow.com/questions/25202147/tkinter-progressbar-with-indeterminate-duration) – mnikley Jun 24 '21 at 08:14
  • I wanted the progress bar start running while the file is being loaded and finished when function code is executed. Your above solution also works but not running when file is loaded when button clicked. – Michael Jun 24 '21 at 08:39
  • Sorry i really do not understand your question. Either you determine the progress manually, or you have a thread or process of some sort with a stop signal (which is not the easiest task to do) which tells the progress bar to stop. I would recommend you determine a global loading_progress variable in your file loading script (can be in a separate .py file), and hand that parameter over to your progress bar value like i explained. – mnikley Jun 24 '21 at 08:55