Building a Progress Bar GUI Application with Python PySimpleGUI – Step-by-Step Guide

Posted by


In this tutorial, we will be creating a progress bar GUI app using PySimpleGUI in Python. PySimpleGUI is a simple Python package that makes it easy to create GUI applications with minimal code. Progress bars are useful in indicating the progress of a task, such as downloading a file or performing a complex computation.

To get started, make sure you have Python installed on your system. You can download Python from the official website (https://www.python.org/) if you don’t have it already. Once you have Python installed, you can install PySimpleGUI using pip:

pip install PySimpleGUI

Now that you have PySimpleGUI installed, let’s create a new Python script and import the necessary modules:

import PySimpleGUI as sg
import time

Next, we will define the layout of our GUI app. We will create a window with a progress bar and a Cancel button. Here is the layout code:

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

Now, let’s create the window using the layout we defined above:

window = sg.Window('Progress Bar App', layout)

Next, we will create a loop that will run until the window is closed. Inside the loop, we will handle events such as button clicks and update the progress bar accordingly:

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

    if event == sg.WIN_CLOSED:
        break

    if event == 'Start':
        for i in range(100):
            if event == 'Cancel':
                break

            window['progressbar'].update_bar(i + 1)
            time.sleep(0.1)

    if event == 'Cancel':
        window['progressbar'].update_bar(0)

In the code above, we check for events such as button clicks and update the progress bar accordingly. When the ‘Start’ button is clicked, the progress bar will update incrementally until it reaches 100. If the ‘Cancel’ button is clicked, the progress bar will reset to 0.

Finally, we will add the following code to close the window when the loop ends and run the app:

window.close()

That’s it! You have successfully created a progress bar GUI app using PySimpleGUI in Python. You can customize the layout and functionality of the app further by adding more elements and event handlers.

I hope this tutorial was helpful in understanding how to create a progress bar GUI app using PySimpleGUI. Happy coding!

0 0 votes
Article Rating
1 Comment
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
@turtlecode
3 months ago

🔴Let's learn how to create website using Python Streamlit!

https://www.youtube.com/playlist?list=PLMi6KgK4_mk2rK5jD-BK5RigFIP2QSq8W