У меня есть код, который извлекает данные из базы данных MySQL с помощью соединителя mysql Python, и я хочу создать индикатор выполнения в Python с помощью tkinter, который показывает ход выполнения запроса.
Я создал цикл, который вычисляет, сколько времени потребуется для выполнения запроса, но я обнаружил, что цикл выполняется отдельно, в то время как другие части кода выполняются отдельно.
Я ожидаю, что цикл будет выполняться одновременно с другие части кода. Я не знаю, правильно ли я делаю это, любая помощь будет оценена по достоинству.
Ниже приведен код, который я использовал
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)
Подробнее здесь: https://stackoverflow.com/questions/788 ... -using-a-p