I have a code that pulls data from mysql database using python mysql connector and I want to create a progress bar in python using tkinter that shows query execution progress.
I have created a loop that calculates how long it will take for the query to execute but I discovered that the loop runs separately while the other parts of the code runs separately.
I expect the loop to run concurrently with other parts of the code. I don’t know if I am doing this the right way, any help will really be appreciated.
Below is the code I used
from tkinter import ttk
import time
import tkinter as tk
from tkinter import filedialog
from tkinter import *
import mysql.connector as sql
import pandas as pd
from datetime import datetime
db_connection = sql.connect(
host='',
database='',
user='',
password='', sql_mode='STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION'
)
def query():
total_rows, df2 = biometrics()
progress_bar['maximum'] = total_rows # Set to 100 for percentage completion
for index, row in df2.iterrows():
# Update the progress bar value
progress_bar['value'] = index + 1
# Calculate the percentage of completion
percentage = ((index + 1) / total_rows) * 100
# Update the status label with the current percentage
status_label.config(text=f"Counting rows to process... {index + 1}/{total_rows} ({percentage:.2f}%)")
# Update the GUI to reflect changes
root.update_idletasks()
# Simulate time-consuming task
time.sleep(0.0001)
db_cursor = db_connection.cursor()
db_cursor.execute(""" #time consuming query here """)
# Creating Main Window
root = tk.Tk()
root.title("QUERY PROGRESS")
root.geometry("650x400")
root.config(bg="#f0f0f0")
MY_button = tk.Button(root, text="CONNECT & GENERATE LINE LIST...", command=query, font=("Helvetica", 14), bg="#4caf50", fg="#ffffff")
convert_button.pack(pady=5)
progress_bar = ttk.Progressbar(root, orient='horizontal', length=300, mode='indeterminate')
progress_bar.pack(pady=15)
# Label for percentage and progress messages
status_label = tk.Label(root, text="0%", font=('Helvetica', 12))
status_label.pack(pady=5)