Creating a Progress Bar with Python PySimpleGUI – Step-by-Step Guide

Posted by


In this tutorial, we will learn how to create a progress bar using the PySimpleGUI library in Python. PySimpleGUI is a powerful yet simple GUI framework that allows you to easily create graphical user interfaces for your Python programs.

To get started, make sure you have PySimpleGUI installed. You can install it using pip:

pip install PySimpleGUI

Now that you have PySimpleGUI installed, let’s create a simple Python script to demonstrate how to create a progress bar.

import PySimpleGUI as sg

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

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

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

    if event == sg.WIN_CLOSED:
        break

    elif event == 'Start':
        for i in range(100):
            window['progress'].update_bar(i + 1)

    elif event == 'Cancel':
        window['progress'].update_bar(0)

window.close()

In this script, we first import the PySimpleGUI library. We then define the layout of our GUI, which consists of a text label, a progress bar, and two buttons (Start and Cancel).

Next, we create a window using the sg.Window function and display the layout that we defined earlier.

Inside the main loop, we handle events using the event variable and the values dictionary. If the user closes the window, we break out of the loop and close the window. If the user clicks the Start button, we update the progress bar in a loop from 1 to 100. If the user clicks the Cancel button, we reset the progress bar to 0.

Finally, we close the window when the loop exits.

That’s it! You now have a basic understanding of how to create a progress bar using PySimpleGUI in Python. Experiment with the code and customize it to suit your needs. Happy coding!

0 0 votes
Article Rating

Leave a Reply

1 Comment
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
@turtlecode
2 hours ago

✅ Python PySimpleGUI Full Playlist : https://www.youtube.com/playlist?list=PLMi6KgK4_mk1kGWXRif1naLejNdYDS_t2

1
0
Would love your thoughts, please comment.x
()
x