Click here to Skip to main content
15,867,594 members
Articles / Programming Languages / Python

Creating User Interfaces in Python using PySimpleGUI

Rate me:
Please Sign up or sign in to vote.
4.97/5 (8 votes)
26 Oct 2021CPOL3 min read 25K   430   11   11
This article demonstrates creating user-interfaces using PySimpleGUI.
Through this article, I wish to demonstrate how we can create good looking user-interfaces using the PySimpleGUI library. Though there are many libraries that can be used for developing Python applications with Graphical User Interfaces, but PySimpleGUI is the simplest one. It is compact, concise and easy to learn even by newbie Python programmers.

Image 1

Introduction

PySimpleGUI is a Python package which can be used to develop user-interfaces with the simplest code. In spite of being simple, it can be used to create complex and sophisticated user-interfaces. Any programmer, even if he is new to Python can easily learn PySimpleGUI and start creating Python applications with good looking user-interfaces within minutes.

Background

PySimpleGUI currently supports 4 GUI frameworks:

  1. PySimpleGUI
  2. PySimpleGUIQt
  3. PySimpleGUIWx
  4. PySimpleGUIWeb

Porting GUI from one framework to another can be done with very few changes. Some features may be supported by one framework but not by others, but overall, porting from one framework to another is easy with minimal changes in code. The framework to be used can be specified using the import statement. For example, to use PySimpleGUI, we can write import PySimpleGUI as sg and to use PySimpleGUIQt, we can write import PySimpleGUIQt as sg and to use PySimpleGUIWeb, we can write import PySimpleGUIWeb as sg.

Using the Code

Like other packages, PySimpleGUI can be installed using the pip command as follows:

pip install pysimplegui --user

To install PySimpleGUIWeb, the following command can be used:

pip install pysimpleguiweb --user

Layouts

A layout is defined in PySimpleGUI using a List of Lists. Each list inside the main list defines one row. Each row consists of user-interface elements which are rendered on a single line.

Following are some of the commonly required user-interface elements that can be created in PySimpleGUI.

  • sg.Text: To render constant text
  • sg.InputText: To accept input at runtime
  • sg.Checkbox: To display a checkbox
  • sg.Radio: To display a radio button
  • sg.Combo: To display a list of items as a combobox from which only one element can be selected
  • sg.Listbox: To display a list of items as a listbox. Setting select_mode="multiple" attribute allows selecting multiple elements

For example, the following code can be used to render a typical user-interface:

Python
layout = [
                [sg.Text("Select a theme: ",size=(15,1)),
                sg.Combo(sg.theme_list(),key="t",default_value="BlueMono",size=(10,1))],
                [sg.Text("Enter your name: ",size=(15,1)),
                sg.InputText(key="n",size=(10,1))],
                [sg.Checkbox("Capitalize",key="c",size=(15,1))],
                [sg.Text("Choose your gender: ",size=(15,1)),
                sg.Radio("Male","g",True,key="g1"),
                sg.Radio("Female","g",key="g2")],
                [sg.Text("Choose your qualification: ",size=(15,1)),
                sg.Combo(qualifications_list,key="q",default_value="Graduate",size=(15,3))],
                [sg.Text("Choose your hobbies: ",size=(15,1)),
                sg.Listbox(hobbies_list,key="h",select_mode="multiple",size=(15,3))],
                [sg.Button("Show",size=(15,1)),sg.Button("Reset",size=(15,1))]
             ]
window = sg.Window("Controls",layout)

The first row in the above layout consists of a combo box containing all available themes, obtained using the sg.theme_list() function. Other rows define other user interface elements. The sg.Window is used to create a window with the title as "Controls" and having the specified layout. Its output is as follows:

Image 2

Event Handling

An event loop is required to process events and read the inputs entered at runtime. The event loop can be created as follows:

Python
while True:
        event,values = window.read()
        if event == sg.WINDOW_CLOSED:
           break
        if event == "Show":
           name = values["n"].upper() if values["c"] else values["n"]
           gender = "Male" if values["g1"] else "Female"
           qualification = values["q"]
           hobbies = ""
           for h in values["h"]:
                hobbies = hobbies + h + ","
           hobbies = hobbies.rstrip(",")
           details = "Name: " + name + "\n"
           details = details + "Gender: " + gender + "\n"
           details = details + "Qualification: " + qualification + "\n"
           details = details + "Hobbies: " + hobbies + "\n"
           sg.theme(values["t"])
           sg.popup(details,title="Details")
        if event == "Reset":
           window["n"].update("")
           window["c"].update(False)
           window["g1"].update(True)
           window["q"].update("Graduate")
           window["h"].update(hobbies_list)
window.close()

In the above code, the information about the button clicked or window closed events and the data entered are obtained using the window.read() function. If the window close button is clicked, the sg.WINDOW_CLOSED event is fired, which causes the control to break out of the event loop and the window is closed using the close() function. If the "Show" button is clicked, the "Show" event is fired, in which all data entered on the form is collected using the values dictionary and displayed in a popup window using the popup() function. If the "Reset" button is clicked, the "Reset" event is fired, in which all controls are reset to their default values using the update() function. In the "Show" event, the currently selected theme is applied using the sg.theme() function.

Image 3 Image 4

Themes: Themes can be used in PySimpleGUI to display colorful and more beautiful windows using the shortest possible code. The following line of code, creates a combo box filled with all available themes.

Python
sg.Combo(sg.theme_list(),key="t",default_value="BlueMono",size=(10,1))

To apply the selected theme from the combo box, the following code can be used.

Python
sg.theme(values["t"])

All available themes can be previewed using the sg.theme_previewer() function as follows:

Python
import PySimpleGUI as sg
sg.theme_previewer()

The above code produces the following output:

Image 5

Points of Interest

I hope the above discussion is helpful to readers and leads more programmers to use PySimpleGUI.

History

  • 27th October, 2021: Initial version

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Instructor / Trainer NIIT, India
India India
I am a trainer by profession. Currently I am working with iFuture Technologies(India) as a Senior Faculty. I enjoy programming as a hobby. During my career I have seen the growth and decline of many technologies, many of them being my favorites like Flash, WPF, Windows Mobile Development. Few of my current favorites are Android, Xamarin and Python, though I also like traditional and evergreen languages like PHP, C#, Visual Basic and Java.

Apart from computers, my favorite pastime is bicycling.

Comments and Discussions

 
GeneralMy vote of 5 Pin
Ștefan-Mihai MOGA21-Nov-21 21:14
professionalȘtefan-Mihai MOGA21-Nov-21 21:14 
QuestionWhat is PySimpleGUIWeb Pin
Pete Lomax Member 106645059-Nov-21 5:18
professionalPete Lomax Member 106645059-Nov-21 5:18 
AnswerRe: What is PySimpleGUIWeb Pin
Azim Zahir12-Nov-21 15:53
Azim Zahir12-Nov-21 15:53 
GeneralMy vote of 5 Pin
Igor Ladnik8-Nov-21 5:11
professionalIgor Ladnik8-Nov-21 5:11 
GeneralRe: My vote of 5 Pin
Azim Zahir8-Nov-21 17:35
Azim Zahir8-Nov-21 17:35 
QuestionPySimpleGUIQt Pin
YDLU3-Nov-21 9:48
YDLU3-Nov-21 9:48 
AnswerRe: PySimpleGUIQt Pin
Azim Zahir6-Nov-21 16:57
Azim Zahir6-Nov-21 16:57 
GeneralRe: PySimpleGUIQt Pin
YDLU9-Nov-21 10:41
YDLU9-Nov-21 10:41 
GeneralRe: PySimpleGUIQt Pin
Azim Zahir12-Nov-21 15:42
Azim Zahir12-Nov-21 15:42 
QuestionMessage Closed Pin
1-Nov-21 16:02
defect tech1-Nov-21 16:02 
GeneralMy vote of 5 Pin
Ștefan-Mihai MOGA31-Oct-21 20:07
professionalȘtefan-Mihai MOGA31-Oct-21 20:07 
GeneralRe: My vote of 5 Pin
Azim Zahir2-Nov-21 18:53
Azim Zahir2-Nov-21 18:53 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.