Introduction to creating apps in Python using PySimpleGUI

Posted by


PySimpleGUI is a Python library that allows you to easily create graphical user interfaces (GUIs) for your Python applications. With PySimpleGUI, you can quickly build and design interactive windows, dialog boxes, and input forms for your programs.

In this tutorial, we will cover the basics of PySimpleGUI and walk you through how to create your first GUI application using this powerful library.

Installing PySimpleGUI

Before diving into creating GUI applications with PySimpleGUI, you need to install the library. You can do this easily using pip, the Python package manager. Open your command prompt or terminal and run the following command:

pip install PySimpleGUI

Creating your first PySimpleGUI application

Now that you have PySimpleGUI installed, let’s create a simple GUI application that displays a window with a text input field and a submit button. Create a new Python file and import the PySimpleGUI library:

import PySimpleGUI as sg

Next, we will define our layout for the GUI window. The layout is a list of lists that specifies the elements you want to display in the window. In this example, we will have a text input field and a submit button:

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

After defining the layout, we can create our window by calling the sg.Window constructor and passing in the title of the window and the layout we defined:

window = sg.Window("Hello World App", layout)

Now, we need to create a loop to listen for user input and events in the window. Inside the loop, we will check for events using the read method and handle any events that occur. In this example, we will display a popup window with the entered text when the submit button is clicked:

while True:
    event, values = window.read()
    if event == sg.WIN_CLOSED or event == "Submit":
        sg.popup("Hello, " + values[0])
        break

Finally, we need to close the window once the user has interacted with it by calling the close method:

window.close()

Running your PySimpleGUI application

You can run your PySimpleGUI application by executing the Python script you just created. The window should appear with the text input field and submit button. Enter your name in the input field and click the submit button to see a popup window displaying a greeting message.

Congratulations, you have created your first GUI application using PySimpleGUI! This is just the tip of the iceberg when it comes to what you can achieve with PySimpleGUI. With its simple and intuitive API, you can build complex and feature-rich GUIs for your Python applications in no time.

Conclusion

In this tutorial, we covered the basics of PySimpleGUI and demonstrated how to create a simple GUI application using the library. PySimpleGUI is a versatile and beginner-friendly tool that makes building GUIs in Python a breeze. Experiment with different layouts, widgets, and event handlers to create custom and interactive GUIs for your projects. Happy coding!

0 0 votes
Article Rating
40 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
@ClearCode
2 months ago

If you enjoy this video and want to support me you can join my Patreon or buy my Udemy course
patreon.com/clearcode
udemy.com/course/learn-python-by-making-games

@MD-cu8tt
2 months ago

Thank you

@bernardseifert3034
2 months ago

they reallly made pysimplegui register to use wasnt it open source?

@Prithvikizhaka
2 months ago

Hey why is the app pop up not coming up for me when i launch my code?

@PySimpleGUI
2 months ago

Fantastic!

@ALMZ17
2 months ago

Stopwatch don’t work after pressing the reset button😢

@JMusicSelection
2 months ago

1:47:00 In the Text Editor the word count is wrong when there are chain of white spaces or new rows. Use split() without arguments to fix it. Cheers!:
clean_text = full_text.replace('n', ' ').split()
if full_text == '':
word_count=0
else:
word_count = len(clean_text)

@Sourcecodemastergoaheadcheater
2 months ago

Work all day and then i wake up work all day and then i wake up code all day😂

@HulaHoopzz
2 months ago

This is amazing, I always wanted to have an option to create an option to create a Dynamic Theme changer event/button. Never found anything on the web, and it's pretty interesting and informative to see your approach!

@harveybains146
2 months ago

Great explanation of this nice GUI

@AhmedBalfaqih
2 months ago

Simply, this course is among the best Python tutorials on YouTube. The pace is great, the explanation is clear, makes it is easy to follow and execute with hardly any mistakes. Thank you.
I just want to add that the weather app location could not be retrieved as the ID has changed. I used this id "oFNiHe" instead

@omudugavu
2 months ago

man can you teach us kivy

@BestMomentsPro
2 months ago

It will be nice if not a huge size of output exe file

@catherine7918
2 months ago

This is nice and all… but how do you download PySimpleGui? I have literally no idea, after using a search engine.

@ranjansanyal
2 months ago

I am trying to work on a table and backend it’s connected to the db. But after making the list empty, the table click event is always true and it’s throwing error, array out of index.

@gerryhoekema2561
2 months ago

Enjoying this tutorial very much and learning a lot!
However, when programming the graph.py progam, i get this error:
AttributeError: module 'matplotlib' has no attribute 'figure'.
Any ideas on a work-around? Thanks!

@Cake-bz5bf
2 months ago

Can't we make all of these with tkinter?

@joedulis
2 months ago

I stumbled upon this video after searching for PySimpleGUI turorial, and I think your tutorial is the best ever – you really explained it in a very simple and organized method and I could understand it a lot better! Thanks you so much, really appreciated it!

@person_on_the_internet99lolxd
2 months ago

I can start the GUI no problem but the theme_menu won't work for me. Can anyone help me? I use a MacBook Air M1

@tunaburn2330
2 months ago

Thank you!