PySimpleGUI is a lightweight and easy-to-use Python library for creating graphical user interfaces (GUIs). It provides a simple and straightforward way to create GUIs for your Python applications without the need for complex code or external dependencies. In this tutorial, we will walk through the process of creating GUIs with PySimpleGUI and Python.
Installing PySimpleGUI:
Before we can start creating GUIs with PySimpleGUI, we need to install the library. PySimpleGUI can be installed using pip, the Python package manager. To install PySimpleGUI, open a terminal or command prompt and run the following command:
pip install PySimpleGUI
Creating a simple GUI:
To create a simple GUI with PySimpleGUI, we first need to import the PySimpleGUI library:
import PySimpleGUI as sg
Next, we can start designing our GUI by defining the layout. The layout is a list of lists that specify the elements and their placement in the GUI. Each element in the layout has a corresponding key that can be used to reference the element later. For example, we can create a simple GUI with a text input field and a submit button like this:
layout = [
[sg.Text('Enter your name:')],
[sg.InputText(key='name')],
[sg.Button('Submit')]
]
Once we have defined the layout, we can create the GUI window by calling the sg.Window
function and passing the layout as an argument:
window = sg.Window('Simple GUI', layout)
Finally, we can interact with the GUI by using a loop to read events and update the GUI window. For example, we can create a loop that reads events from the GUI window and performs an action when the submit button is clicked:
while True:
event, values = window.read()
if event == 'Submit':
name = values['name']
sg.popup(f'Hello, {name}!')
if event == sg.WIN_CLOSED:
break
window.close()
This code will create a simple GUI window with a text input field for entering a name and a submit button. When the submit button is clicked, a popup window will display a greeting message with the entered name. The GUI window will close when the user clicks the close button.
Adding more elements to the GUI:
PySimpleGUI provides a variety of elements that can be added to a GUI window, such as buttons, text inputs, sliders, checkboxes, and more. Each element has its own properties and methods that can be customized to suit your needs. For example, we can add a dropdown menu and a checkbox to the previous example like this:
layout = [
[sg.Text('Enter your name:')],
[sg.InputText(key='name')],
[sg.Button('Submit')],
[sg.Text('Select a color:')],
[sg.Drop(values=['Red', 'Green', 'Blue'], key='color')],
[sg.Checkbox('I agree to the terms and conditions', key='agree')],
]
We can then add logic to handle events from the new elements in the GUI window:
while True:
event, values = window.read()
if event == 'Submit':
name = values['name']
color = values['color']
agree = values['agree']
message = f'Hello, {name}! You selected {color}.'
if agree:
message += ' Terms and conditions accepted.'
sg.popup(message)
if event == sg.WIN_CLOSED:
break
window.close()
This code will create a GUI window with additional elements, including a dropdown menu for selecting a color and a checkbox for agreeing to terms and conditions. The greeting message will now also include the selected color and a confirmation of the terms and conditions acceptance.
Customizing the GUI appearance:
PySimpleGUI allows you to customize the appearance of your GUI windows by setting properties such as the window title, size, background color, font style, and more. For example, we can customize the appearance of the GUI window in the previous example by adding the window_title
and size
properties to the sg.Window
function:
window = sg.Window('Customized GUI', layout, window_title='My GUI', size=(300, 200))
We can also set the background color of the GUI window by adding the background_color
property to the layout elements:
layout = [
[sg.Text('Enter your name:', background_color='white')],
[sg.InputText(key='name')],
[sg.Button('Submit')],
[sg.Text('Select a color:', background_color='white')],
[sg.Drop(values=['Red', 'Green', 'Blue'], key='color')],
[sg.Checkbox('I agree to the terms and conditions', key='agree', background_color='white')],
]
These customization options allow you to create visually appealing GUI windows that match the style and branding of your application.
Conclusion:
In this tutorial, we have covered the basics of creating GUIs with PySimpleGUI and Python. We have learned how to install PySimpleGUI, design GUI layouts, interact with GUI elements, customize the appearance of GUI windows, and handle events. With PySimpleGUI, you can quickly and easily create intuitive and user-friendly interfaces for your Python applications. I hope this tutorial has been helpful, and I encourage you to explore the PySimpleGUI documentation and examples to further enhance your GUI development skills.
Thank you for how you explicitly explained this, you really took your time and had it properly organized. i gained a lot during this brief moments and am grateful
Nice video. You said in the video taht pysimpleGUI can wrap Pyside or wxPython, but do the code change in that cases, or can you still use same layouts, etc…?
I settled for PyQt years ago. It does all the Python GUI code for me. And I like that it uses either Windows or MacOS widgets, or you can customize the style of your PyQt GUI. I use matplotlib with PyQt.
This is my most favourite library. I have created at least 20+ projects using this in Amazon for non tech team members in last 3 years