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.
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')