Tutorial 1: Building GUI Applications with PySimpleGUI in Python

Posted by


Creating GUI apps with Python PySimpleGUI is a great way to quickly build interactive user interfaces for your Python programs. In this tutorial, we will go over the basics of creating a simple GUI app using PySimpleGUI.

Step 1: Install PySimpleGUI
The first thing you need to do is install PySimpleGUI. You can do this by running the following command in your terminal or command prompt:

pip install PySimpleGUI

Step 2: Import PySimpleGUI
Next, you need to import PySimpleGUI in your Python code. You can do this by adding the following line at the top of your script:

import PySimpleGUI as sg

Step 3: Create a window
To create a simple window with PySimpleGUI, you can use the sg.Window() function. This function takes in a title for the window and a layout, which defines the elements that will be displayed on the window.

Here’s an example of how to create a simple window with a text input field and a submit button:

layout = [
    [sg.Text('Enter your name:'), sg.InputText()],
    [sg.Button('Submit')]
]

window = sg.Window('Simple GUI App', layout)

Step 4: Read events
After creating the window, you need to read events from the window using the window.read() method. This method will block the program execution until an event occurs, such as a button click.

Here’s an example of how to read events from the window and perform an action when the submit button is clicked:

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

    if event == sg.WIN_CLOSED or event == 'Exit':
        break

    if event == 'Submit':
        name = values[0]
        sg.popup('Hello, ' + name + '!')

Step 5: Display the window
Finally, you need to display the window using the window.finalize() method. This will display the window on the screen and allow the user to interact with the elements on it.

Here’s the complete code for creating a simple GUI app that prompts the user to enter their name and displays a greeting message:

import PySimpleGUI as sg

layout = [
    [sg.Text('Enter your name:'), sg.InputText()],
    [sg.Button('Submit')]
]

window = sg.Window('Simple GUI App', layout)

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

    if event == sg.WIN_CLOSED or event == 'Exit':
        break

    if event == 'Submit':
        name = values[0]
        sg.popup('Hello, ' + name + '!')

window.close()

That’s it! You have successfully created a simple GUI app using PySimpleGUI. You can explore more features and layouts provided by PySimpleGUI to build more complex and interactive GUI apps. I hope this tutorial was helpful, and happy coding!

0 0 votes
Article Rating
7 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
@turtlecode
3 months ago
@neilianedles9158
3 months ago

To make good use of it , you have to pay 🙁

@mr.RAND5584
3 months ago

Nice voice.

@ch0np5
3 months ago

so im getting an error that says pysimplegui doesnt have the attribute 'text', meaning that the library wasnt fully imported, how do i do that correctly?

@zartosht06
3 months ago

Hello, getting this error message.

WARNING: Secure coding is not enabled for restorable state! Enable secure coding by implementing NSApplicationDelegate.applicationSupportsSecureRestorableState: and returning YES.

Apparently I need to install it but don't kn ow how.

@hakanbilgin6875
3 months ago

Thank you for video

@OumarouCoulibaly-fg5pk
3 months ago

Hi, thanks for your training videos. I would like to become a member of your channel in order to watch the members-only videos please.