Graphical User Interface (GUI)
Here are resources that you can use to build your desktop graphical user interface (GUI) project:
- PySimpleGUI: Library – Python GUI For Humans
- PySimple GUI: User’s Manual – Documentation and demos
- PySimpleGUI: The Simple Way to Create a GUI With Python – Real Python tutorial article
Next, let’s take a look at the application program for a desktop GUI. In this section, the same application is going to be programmed for a GUI. In this case, I’m going to use the
00:13 This has been chosen as it provides the simplest access possible to a native GUI. There are more complicated ways to access it, but generally, these will involve more code to get your project up and running.
But as you can see, even when using a simplified library, the overhead of the amount of code you have to write is significantly more than when just using the command-line interface. Let’s get started. First up, importing the
So there, you can see that all the needed elements have been created in the list of lists. There’s this
Text element, which will be the label for this first input, and then largely the same with different text for the second line, and on the third line, there are two buttons—one of them called
'Add' and the other one called
'Quit'. Next, a window is created, and this is using a
PySimpleGUI.Window object. Here, it’s passed the title of the
Window and the
layout we created previously. Next, the structure of the program will be a
while loop, where
values are read from the
So, here’s an
if statement with comparisons to either
'Quit', so if the Quit button is pressed or the window is closed, then the loop will be broken. And here, if the Add button has been pressed, the values will be taken and these are passed as the dictionary
values, so the first one has a key of
0. And as you can see, the second one has a key of
1. A simple addition is performed.
In this case, a
PySimpleGUI.Popup is going to be used to provide the user with the result. As you can see, that part of the code looks similar to the print statement from the previous version. Finally, if the loop has been exited, we’ll close the
Window. So, let’s save that code and then see it in action.
Become a Member to join the conversation.