How do I get the Tkinter progressbar to work with database lists?

I am almost done building my URL checker, which loops through a list of URLs stored in a database and check if they are valid and working.

What I want now is to have a progressbar, so that when I fetch the URL list from my database and loop through it to validate each URL, I want to see the progress from start to finish.

I read about the Tkinter progressbar and followed the tutorial for it, but I am not sure how I can integrate it into my code.

The list does not have a fixed value. Right now it’s 1000, but it might be more or less depending on what URLs get deleted and which get added later on, so I can’t say that 1000 equals 100%. Also I can’t use Range, since I am not dealing with integers.

I need to somehow tell the program that the current amount of values in my database equals to 100%, and then make the progressbar move as I work through each of those values in the row that I am checking.

win = Tk()
win.geometry("620x400")

def URL_Validator():
    # Check drive
    for driver in pyodbc.drivers():
        print(driver)

    # Define server and database
    DRIVER = '{ODBC Driver 17 for SQL Server}'
    SERVER = '...\MSSQLSERVER01'
    DATABASE = 'TEST_DATABASE'

    # Create connection string
    CONNECTION_STRING = "Driver={driver}; " \
                        "Server={server}; " \
                        "Database={database}; " \
                        "Trusted_Connection=yes;" \
        .format(driver=DRIVER, server=SERVER, database=DATABASE)

    # Connect to database
    connection_object: pyodbc.Connection = pyodbc.connect(CONNECTION_STRING)

    # Create cursor object
    cursor_object: pyodbc.Cursor = connection_object.cursor()

    # Read primary URL from database

    # Fetch records from database
    sql_select = "SELECT Url FROM Table_4"
    records = cursor_object.execute(sql_select).fetchall()

    # Loop through the database records

    for row in records:
        if row:
            # Check if URL in first row is valid
            if validators.url(str(row[1])):
                print(f"{row[1]} - Url is valid")

                try:
                    get = requests.get(row[1], timeout=10)
                    if get.status_code == 200:
                        print(f"{row[1]}: is reachable")
                    else:
                        print(f"{row[1]}: is Not reachable")
                except BaseException as error:
                    print('An exception occurred: {}'.format(error))

        bar['value'] += 1
        win.update_idletasks()

bar = Progressbar(win, orient=HORIZONTAL, length=300)
bar.pack(pady=20)
Button(win, text="Check URL", command=database_connector).pack(pady=20)
win.mainloop()

You can actually add a counting variable inside your for loop which iterates through every data inside the rows and just increment it. That would give you the length of the real-time length of the list, which you can use to provide the 100% to the progressbar.

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.