Creating Basic UI Elements
00:00 Creating Basic UI Elements in PySimpleGUI. If you’ve ever used a GUI toolkit before, then you may have heard the term widgets. A widget is a generic term used to describe the elements that make up the user interface, such as buttons, labels, windows, and so on. In PySimpleGUI, widgets are referred to as elements, which you may sometimes see capitalized.
Window() takes lots of different arguments—too many for me to cover here. However, for this example you can give the
layout, and set the
margins, which is how big the UI window will be in pixels.
But if you’ve ever tried GUI programming before, you realize that this is an achievement in itself. Normally, you would have other elements besides a
Window() in the application, so let’s add some text and a button into the mix.
Most GUI toolkits allow you to lay out the elements using absolute positioning or by allowing the GUI to lay them out dynamically. PySimpleGUI uses nested Python lists to lay out its elements. In this case, you add a
Text() element and a
02:34 The last block of code here is the event loop. A graphical user interface needs to run inside a loop and wait for the user to do something. For example, the user might need to press a button in your UI or type something with their keyboard. When they do that, those events are processed by the event loop.
When you use PySimpleGUI, you make an event loop by creating an infinite
while loop that reads events from the
window object. If the user presses the OK button or the Exit button, then you want the program to end. To accomplish that, you break out of the loop and close the
Become a Member to join the conversation.