How to Build an Advanced GUI App Using PySimpleGUI: A Comprehensive Tutorial

Posted by


In this tutorial, we will create an advanced GUI app using PySimpleGUI, which is a simple yet powerful Python library for creating graphical user interfaces. We will walk through the process of creating a fully functional app with multiple GUI elements such as buttons, input fields, dropdown menus, checkboxes, and more.

Step 1: Install PySimpleGUI

First, you need to install PySimpleGUI on your system. You can do this using pip by running the following command in your terminal:

pip install PySimpleGUI

Step 2: Import the necessary modules

Next, we need to import the PySimpleGUI module in our Python script. We will also import any other modules that we need for our app, such as sys for handling command-line arguments.

import PySimpleGUI as sg
import sys

Step 3: Define the layout of the GUI

Now, we will define the layout of our GUI app using PySimpleGUI elements. The layout is a list of lists, where each inner list represents a row of elements in the GUI. Here is an example layout that includes a text input field, a submit button, and a label to display the result:

layout = [
    [sg.Text('Enter your name:'), sg.InputText()],
    [sg.Button('Submit')],
    [sg.Text(size=(40, 1), key='output')]
]

Step 4: Create the window

Next, we will create a PySimpleGUI window object with our defined layout and set some window properties, such as the window title and size.

window = sg.Window('My GUI App', layout, size=(400, 150))

Step 5: Event loop

Now, we will enter the event loop, where we will handle user input and update the GUI elements accordingly. We will continuously read events from the window until the user closes the window.

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

    if event == sg.WIN_CLOSED:
        break

    if event == 'Submit':
        name = values[0]
        window['output'].update(f'Hello, {name}!')

Step 6: Close the window

Finally, we need to close the window and cleanup any resources used by the app.

window.close()

And that’s it! You have now created an advanced GUI app using PySimpleGUI. Feel free to customize the layout and add more GUI elements to your app to make it more interactive and user-friendly. Happy coding!

0 0 votes
Article Rating

Leave a Reply

44 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
@CodingIsFun
2 days ago

What kind of programs are you planning to build with the PySimpleGUI library? 🤓

@dudeCoding
2 days ago

Great tutorial. How large was the .exe file in the end? mine is 38,297 kb. Thank you!

@dudeCoding
2 days ago

at 12:46 when you create a config file, it might save as a config.ini.txt file. if it does, open the file, go to save as, "config.ini". this will convert the text document to an ini file! Took me a while to figure this out.

@tobiewaldeck7105
2 days ago

Hi. Why is my pyinstaller applications being flagged by antivirus software. I am switching to a new UI and I want to sell my applications, but I don't know how long that would last if my software get flagged by antivirus.

@leonardolozzi
2 days ago

Can i put an image instead text for substitue text keywords in Word?

@normag6769
2 days ago

Sven, I quick question: for this example, are you using pysimplegui version 5 or 4?

@juancharlie777
2 days ago

Excellente! I just subscribed.

@koosschutter1675
2 days ago

pysimplegui will now be moving to a subscription model if you can believe that. Do you think that the older version 4, if installable, could still be used without problem or interference?

@animatorslife9733
2 days ago

Wanted a beginner gui python library because tkinter felt old looking. Safe to say, landed in the right place!

@hodifa_almosa
2 days ago

❤❤❤

@vladimirkostov8182
2 days ago

Hello, your videos about PySimpleGUI are awesome! I would like to ask you if you know, is it possible to insert invoice template made in excel in pysimplegui app and for example when you press a button named " Preview of your invoice " to show popup window with the template that have been made in excel? Or something like that? Thanks in advance!

@ramchillarege1658
2 days ago

Nice demo. Thanks

@jaydenjonathanowen1803
2 days ago

u know u could just make a simple app using

import PySimpleGUI as sg
win = sg.Window(title="app",
layout=[[]],
size=(400, 200))

win.read()
win.exit()

@joedulis
2 days ago

Thank you so much for your video! Your tutorial is so easy to follow, thanks!

@JyskMaker
2 days ago

Hi love this guide and used it to start learning Python. 🙂
Can some body help me here..
i have a test.ini file with this in it.

[TEST]
1 = 0,1,2,3
2 = 3,2,1
4 = A,B,C
5 = A,1,2,B, 3,2
AL = Temp, Man

Is there a way to read the ini file like READ["TEST"] and get the 1,2,4,5, AL as a result?
PS trying to learn to program python.

@tanmaygoyal8150
2 days ago

Hey. This was super helpful. But I am facing one issue here. I run the final script, click on settings -> save current settings -> ok (in the popup) and instead of closing only the settings window, this closes the main window as well. However if I do not include the popup feature in the settings window, and then click on save current settings button, then it works as expected and only closes the settings window.
Realised that it does not actually close the main window but the main window goes into background processes. This is very undesirable. PLEASE HEL.P

@tagliarini
2 days ago

Amazing!

@Sol_Naif
2 days ago

Thank you so much for your excellent and unique explanation. Thank you from the bottom of my heart.

@PinkPixelRabbit
2 days ago

It seems that psgcompiler doesn't work on MacOS… Anyone got the same issue? How can I convert it to a .app or .dmg?

@PinkPixelRabbit
2 days ago

This channel is crazy… your tutorials and ideas are even more valuable than those paid courses on Udemy! Thanks for making these tutorials Sven 😇

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