0

I have some code where I iterate over a dataframe Tickers. It contain 10 rows (based on the Nr_of_Tickers to make it dynamic) and I would like to add a progress bar that is changing for each iteration. I've got the label number to change after each iteration, but my green progress bar doesn't follow the iteration progress.

I've tried to change this part of the code to sync the green progress bar, but I haven't really catch the logic yet to make it work.

for i in range(idx):
  sleep(0.1)

Any ideas how to adjust the progress bar?

As can be seen in the picture the green progress bar is way of. enter image description here

Example code to copy & paste and replicate issue:

import pandas as pd
import numpy as np
from datetime import datetime

# https://www.askpython.com/python-modules/tkinter/tkinter-spinbox-and-progressbar-widgets # source code 1
# https://stackoverflow.com/questions/47896881/progressbar-with-percentage-label # source code 2
# importing tkinter module 
from tkinter import *
from tkinter import ttk
from tkinter.ttk import Progressbar
import time
import tkinter as tk


from tkinter.ttk import Progressbar, Style, Button
from time import sleep

## Progress bar settings ###########################
# Create the master object
master = tk.Tk()
 
style = ttk.Style(master)
# add label in the layout
style.layout('text.Horizontal.TProgressbar', 
             [('Horizontal.Progressbar.trough',
               {'children': [('Horizontal.Progressbar.pbar',
                              {'side': 'left', 'sticky': 'ns'})],
                'sticky': 'nswe'}), 
              ('Horizontal.Progressbar.label', {'sticky': ''})])
# set initial text
style.configure('text.Horizontal.TProgressbar', text='0 %')

# create progressbar
variable = tk.DoubleVar(master)

# Create a progressbar widget
progress_bar = ttk.Progressbar(master, style='text.Horizontal.TProgressbar', orient="horizontal",
                              mode="determinate", maximum=100, value=0, variable=variable, length = 150)

# change the text of the progressbar, 
# the trailing spaces are here to properly center the text
style.configure('text.Horizontal.TProgressbar', text="0 %      ")

def increment():
    for i in range(idx):
      sleep(0.1)
      progress_bar.step()  # increment progressbar 
      style.configure('text.Horizontal.TProgressbar', text='{:02.1f} %      '.format(((idx+1)/Nr_of_Tickers)*100))
      master.update()
      


# And a label for it
label_1 = tk.Label(master, text='Progress Bar: ')
  
# Use the grid manager
label_1.grid(row=0, column=0)
progress_bar.grid(row=0, column=1)


####################################################



Tickers = pd.DataFrame({'Stock ID': [1, 2, 3, 4, 5, 6, 7, 8, 9, 10], 'colors': ['red', 'white', 'blue', 'yellow', 'pink', 'grey', 'green', 'cyan', 'magenta', 'orange']})
# print(Tickers)

Colores = []
Nr_of_Tickers = Tickers.shape[0]
print(Nr_of_Tickers)

# Progress Bar
while progress_bar['value'] < Nr_of_Tickers:
## starting the loop
    for idx, row in Tickers.iterrows():
        increment()

        Stock_ID_row = Tickers.loc[idx, 'Stock ID']
        Colores = Tickers.loc[idx, 'colors']
        print(Stock_ID_row)
        print(Colores)       

print('Done')
Wizhi
  • 6,424
  • 4
  • 25
  • 47
  • 1
    Off the top of my head, the `pbar` is only sticky "ns". Have you tried changing it to something else? – Bryan Oakley Jul 10 '20 at 16:16
  • Yes, I have. I have tried different combination of: `“n”, “s”, “w”, or “e”`. Unfortunately non of them solved my progress bar issue. But I solved my percentage label at least :). But still the progress bar is not in sync. – Wizhi Jul 10 '20 at 16:33

1 Answers1

0

So the issue i had was that I didn't declared how many step my bar should increase for every iteration. progress_bar.step() -> progress_bar.step(x) -> progress_bar.step(increment_step)

So I declare a variable increment_step with the amount of step the bar should increase for each iteration and then I use that variable for my progress bar. Notice that the variable should be a constant (the bar should increase the same amount over the total number of iteration (Nr_of_Tickers)).

So full working example code is below:

# https://www.askpython.com/python-modules/tkinter/tkinter-spinbox-and-progressbar-widgets
# https://stackoverflow.com/questions/47896881/progressbar-with-percentage-label
# importing tkinter module 
from tkinter import *
from tkinter import ttk
from tkinter.ttk import Progressbar
import time
import tkinter as tk


from tkinter.ttk import Progressbar, Style, Button
from time import sleep

## Progress bar settings ###########################
# Create the master object
master = tk.Tk()
 
style = ttk.Style(master)
# add label in the layout
style.layout('text.Horizontal.TProgressbar', 
             [('Horizontal.Progressbar.trough',
               {'children': [('Horizontal.Progressbar.pbar',
                              {'side': 'left', 'sticky': 'ns'})],
                'sticky': 'nswe'}), 
              ('Horizontal.Progressbar.label', {'sticky': ''})])
# set initial text
style.configure('text.Horizontal.TProgressbar', text='0 %')

# create progressbar
variable = tk.DoubleVar(master)

# Create a progressbar widget
progress_bar = ttk.Progressbar(master, style='text.Horizontal.TProgressbar', orient="horizontal",
                              mode="determinate", maximum=100, value=0, variable=variable, length = 150)

# change the text of the progressbar, 
# the trailing spaces are here to properly center the text
style.configure('text.Horizontal.TProgressbar', text="0 %      ")

increment_step = []

def increment():
    for i in range(1):
      increment_step = (((1)/Nr_of_Tickers)*100)
      print(increment_step)
      sleep(0.5)
      progress_bar.step(increment_step)  # increment progressbar 
      style.configure('text.Horizontal.TProgressbar', text='{:02.1f} %      '.format(((idx+1)/Nr_of_Tickers)*100))
      #progress_bar.update
      master.update()


# And a label for it
label_1 = tk.Label(master, text='Progress Bar: ')
  
# Use the grid manager
label_1.grid(row=0, column=0)
progress_bar.grid(row=0, column=1)


####################################################



Tickers = pd.DataFrame({'Stock ID': [1, 2, 3, 4, 5, 6, 7, 8, 9, 10], 'colors': ['red', 'white', 'blue', 'yellow', 'pink', 'grey', 'green', 'cyan', 'magenta', 'orange']})
# print(Tickers)

Colores = []
Nr_of_Tickers = Tickers.shape[0]
print(Nr_of_Tickers)

for idx, row in Tickers.iterrows():
    increment()

    Stock_ID_row = Tickers.loc[idx, 'Stock ID']
    Colores = Tickers.loc[idx, 'colors']
    print(Stock_ID_row)
    print(Colores)       

print('Done')
Wizhi
  • 6,424
  • 4
  • 25
  • 47