3 Examples of Loading Bars in PySimpleGUI

Posted by


In this tutorial, we will explore how to create loading bars using PySimpleGUI. Loading bars are a great way to show the progress of a task to the user and give them an idea of how much time is remaining. We will provide three examples of loading bars with different styles and functionalities.

Step 1: Installing PySimpleGUI

Before we start, make sure that you have PySimpleGUI installed on your system. If you haven’t installed it yet, you can do so by running the following command:

pip install PySimpleGUI

Step 2: Creating a Simple Loading Bar

In this example, we will create a simple loading bar that increments by 10% every second. Here’s the code for the simple loading bar:

import PySimpleGUI as sg
import time

layout = [
    [sg.Text('Processing...')],
    [sg.ProgressBar(1000, orientation='h', size=(20, 20), key='progressbar')]
]

window = sg.Window('Simple Loading Bar', layout)

progress_bar = window['progressbar']

for i in range(1000):
    time.sleep(0.1)
    progress_bar.UpdateBar(i+1)

window.close()

In this code, we first import the necessary libraries and create a layout with a text label and a progress bar. We then create a window with this layout and update the progress bar in a loop using the UpdateBar method. Finally, we close the window once the loop is finished.

Step 3: Creating a Custom Loading Bar

In this example, we will create a custom loading bar that updates based on user input. Here’s the code for the custom loading bar:

import PySimpleGUI as sg

layout = [
    [sg.Text('Progress:')],
    [sg.ProgressBar(100, orientation='h', size=(20, 20), key='progressbar')],
    [sg.Button('Start'), sg.Button('Cancel')]
]

window = sg.Window('Custom Loading Bar', layout)

while True:
    event, values = window.read()

    if event == sg.WIN_CLOSED:
        break
    if event == 'Start':
        for i in range(100):
            window['progressbar'].UpdateBar(i+1)
    if event == 'Cancel':
        window['progressbar'].UpdateBar(0)

window.close()

In this code, we create a layout with a text label, a progress bar, and two buttons for starting and canceling the loading bar. We then create a window and update the progress bar based on user input using the UpdateBar method.

Step 4: Creating an Indeterminate Loading Bar

In this final example, we will create an indeterminate loading bar that continuously loops without a set progress value. Here’s the code for the indeterminate loading bar:

import PySimpleGUI as sg

layout = [
    [sg.Text('Processing...')],
    [sg.ProgressBar(100, orientation='h', size=(20, 20), key='progressbar')]
]

window = sg.Window('Indeterminate Loading Bar', layout)

progress_bar = window['progressbar']

while True:
    event, values = window.read()

    if event == sg.WIN_CLOSED:
        break

window.close()

In this code, we create a layout with a text label and an indeterminate progress bar that continuously loops without a set progress value. We then create a window and close it when the user closes the window.

These are three examples of loading bars that you can create using PySimpleGUI. You can customize these examples further based on your requirements by adding more features or functionalities. Happy coding!

0 0 votes
Article Rating
2 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
@amanqos
1 month ago

Please Sir can you do one video showing Progress bar while downloading video?

@contraplano3157
1 month ago

I did not know how to change color in progress bar, now i did….
to increase speed of bubble it is possible to create a thread to do it faster, splitting numbers in to and join again?