Getting Started with PySimpleGUI: Part 1 – Creating Your First Application

Posted by


Welcome to the first part of the Intro to PySimpleGUI tutorial series! PySimpleGUI is a simple yet powerful Python library for creating graphical user interfaces (GUI) that are cross-platform and easy to use. In this tutorial, we will walk through creating your first GUI application using PySimpleGUI.

  1. Installing PySimpleGUI:

Before we start, make sure you have PySimpleGUI installed on your system. You can install it using pip by running the following command in your terminal or command prompt:

pip install PySimpleGUI
  1. Creating your first PySimpleGUI application:

Now that you have PySimpleGUI installed, let’s create a simple GUI application. We will create a window with a button that displays a message when clicked.

import PySimpleGUI as sg

# Define the layout of the window
layout = [
    [sg.Text('Hello, PySimpleGUI!')],
    [sg.Button('Click me')]
]

# Create the window
window = sg.Window('My First PySimpleGUI App', layout)

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

    if event == sg.WINDOW_CLOSED:
        break

    if event == 'Click me':
        sg.popup('Hello, World!')

# Close the window
window.close()

Let’s break down the code:

  • We import the PySimpleGUI library and alias it as sg for convenience.
  • We define the layout of the window using a list of lists. Each list represents a row in the window, and each element in the list represents a GUI element like a text label or a button.
  • We create the window using sg.Window and pass in the title of the window and the layout.
  • We enter an event loop that listens for events from the window. The window.read() function returns the event that triggered the loop and any values from the window elements.
  • We check if the window was closed and break out of the loop if it was.
  • We check if the event was triggered by clicking the button and display a popup message if it was.
  • Finally, we close the window when we are done.
  1. Running the application:

Save the code to a Python file (e.g., first_app.py) and run it using your Python interpreter. You should see a window with a text label saying "Hello, PySimpleGUI!" and a button labeled "Click me". Clicking the button should display a popup message saying "Hello, World!".

Congratulations! You have just created your first PySimpleGUI application. Stay tuned for the next part of the tutorial series where we will explore more advanced features of PySimpleGUI.

0 0 votes
Article Rating

Leave a Reply

5 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
@DanielSaldivarSalas
19 days ago

what is that tool you are using to highlight your screen?

@KelleysVideos2011
19 days ago

Is the code for this series available? Thanks

@orgon5352
19 days ago

the coding starts at 5:25 😀

@lutetube88
19 days ago

Hi, thanks for the tutorials, much appreciate! I'm new to Python programming and have one question in regard to your tutorials. I see you are using Mac, with the code you show in the tutorials will they work in Windows PC or will they need to be in a slightly different coding? Thanks.

@ricksonbek8775
19 days ago

Nicely done, well explained every step. God bless you

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