We all know the difference a GUI makes. For beginners, a GUI provides an accessible way to use tools that might otherwise seem intimidating when presented through command line interfaces. PySimpleGUI is a lightweight and user-friendly GUI framework for Python. This allows you to create interactive applications with minimal code. Unlike Tkinter, PyQt, or Kivy, PySimpleGUI simplifies graphical user interface (GUI) development by providing a unified API that works with multiple GUI backends.
Today at PythonCentral, let us explain what PySimpleGUI is, how to install it, how to create GUI applications, and use its powerful features. Get. Set. Learn!
How to Install PySimpleGUI
As usual, let us use our favourite package manager Pip, to install PySimpleGUI:
pip install pysimplegui
Instructions to Create a Simple GUI with PySimpleGUI
A basic PySimpleGUI program consists of these components:
- Layouts: Defines the elements such as buttons, text, inputs, and images.
- Windows: Displays the UI and handles events.
- Event loop: Listens for user interactions.
Sample Basic PySimpleGUI Application
Let us look at a basic sample PythonGUI application script:
import PySimpleGUI as sg # This section defines the layout layout = [[sg.Text("Enter your name:"), sg.InputText()], [sg.Button("Submit"), sg.Button("Exit")]] # Here is how you create a window window = sg.Window("Simple GUI", layout) # This section is for the event loop while True: event, values = window.read() if event == sg.WINDOW_CLOSED or event == "Exit": break elif event == "Submit": sg.popup(f"Hello, {values[0]}!") # And finally let us close the window window.close()
Features of PySimpleGUI
Now that we have learnt how to install PySimpleGUI and the basics, let us look at some of the key features.
Widgets and Elements
PySimpleGUI provides various elements for building interfaces. Here are some of them:
Text Elements: sg.Text("Hello from PythonCentral") Input Fields: sg.InputText() Buttons: sg.Button("Click Me") Checkboxes: sg.Checkbox("Option 1") Combo boxes: sg.Combo(["Option 1", "Option 2"]) Progress Bars: sg.ProgressBar(100, orientation='h')
How to Customize Windows
You can customize the window title, size, and background colour. If you would like a specific size for your window with a light blue background, here is how you can do it:
window = sg.Window("Customized GUI", layout, size=(300, 200), background_color="lightblue")
File and Folder Dialogs
If you would like to customize file and folder dialog boxes, adjust this script to your requirements:
file_path = sg.popup_get_file("Select a file") folder_path = sg.popup_get_folder("Select a folder") print("File Selected:", file_path)
Advanced Features in PySimpleGUI
Let us now look at a few advanced features of PySimpleGUI with sample scripts.
How to Handle Events and User Input
PySimpleGUI makes event handling simple with "window.read()".
while True: event, values = window.read() if event in (sg.WINDOW_CLOSED, "Exit"): break if event == "Submit": sg.popup(f"You entered: {values[0]}")
Displaying Images
If you would like to display an image with PySimpleGUI, here is how you can do it:
layout = [[sg.Image("image.png")], [sg.Button("Close")]] window = sg.Window("Image Viewer", layout)
How to Add Graphs and Charts
Most of Python users work with data, graphs, and charts occasionally. Here is how you can insert charts and graphs:
layout = [[sg.Graph(canvas_size=(200, 200), graph_bottom_left=(0, 0), graph_top_right=(100, 100), key="graph")], [sg.Button("Close")]] window = sg.Window("PythonCentral Database Example", layout)
PySimpleGUI vs Other GUI Frameworks
We have learnt about PySimpleGUI. But it is not the only GUI framework available. Let us see how PySimpleGUI competes against its competitors.
In terms of ease of use, PySimpleGui is much better than Tkinter, PyQt, and Kivy. When it comes to performance, they are all almost the same. All these GUI frameworks come with cross-platform compatibility and almost the same level of flexibility. The main reason why PySimpleGui is the community favourite is that the UI is much simpler than Tkinter and others.
Projects for You
Let us take a practice task now. Try to build a to-do list app with event loop. You can use your creativity to customize the dialog boxes, buttons, and all UI elements. Once you are done, compare it with our samples here and see where your creativity has taken you.
To-Do List Layout
Here is the script for a simple to-do list layout.
layout = [[sg.Text("To-Do List")], [sg.InputText(key="task"), sg.Button("Add")], [sg.Listbox(values=[], size=(30, 6), key="tasks")], [sg.Button("Remove"), sg.Button("Exit")]]
Event Loop
This script is for the event loop.
window = sg.Window("To-Do App", layout) tasks = [] while True: event, values = window.read() if event in (sg.WINDOW_CLOSED, "Exit"): break if event == "Add": tasks.append(values["task"]) window["tasks"].update(tasks) if event == "Remove" and values["tasks"]: tasks.remove(values["tasks"][0]) window["tasks"].update(tasks) window.close()
Wrapping Up
PySimpleGUI is an excellent framework for creating desktop applications with Python. Its API support, ease of use, very simple UI, and powerful features make it ideal for both beginners and professionals. We sincerely hope you have learnt everything about it. Start building powerful and interactive GUI applications immediately!