In this lesson we want to learn Python PySimpleGUI: A Beginner’s Guide to GUI Development.
What is Python ?
Python is powerful and powerful programming language that is widely used for different applications, from web development to machine learning. one area where Python shines is GUI development, and PyGTK is one of the most popular libraries for creating graphical user interfaces in Python. in this lesson we are going to explore the basics of PySimpleGUI and how it can be used to create beautiful, functional, and user-friendly GUI applications.
What is PySimpleGUI ?
PySimpleGUI is Python library for GUI development that aims to make GUI programming accessible to users of all skill levels. this library provides easy to use API for creating different range of GUI elements, from simple buttons to complex multi tabbed interfaces. PySimpleGUI is built on top of the Tkinter library, which is the standard GUI library for Python.
Why Use PySimpleGUI ?
There are several reasons why you might choose to use PySimpleGUI for your GUI development projects:
- Easy to use: PySimpleGUI is designed to be easy to use, with focus on simplicity and accessibility. this library provides high level API that makes it easy to create GUI applications without needing to write a lot of code.
- Cross-platform compatibility: PySimpleGUI is compatible with Windows, MacOS and Linux systems, and it is great choice for developers who want to create applications that can run on multiple platforms.
- Large community: PySimpleGUI is an open-source library with a growing community of developers, which means that you’ll have access to a wealth of resources and support when you’re working with the library.
Getting Started with PySimpleGUI
For create GUI Apps with PySimpleGUI, you will need to have Python and PySimpleGUI library installed on your system. the easiest way to install PySimpleGUI is using package manager like pip. Once you have PySimpleGUI installed, you can start writing your first PySimpleGUI application.
Here’s a simple example of a PySimpleGUI program that displays a window with a button:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
import PySimpleGUI as sg def on_button_clicked(): print("Button was clicked") layout = [ [sg.Text("Hello, GeeksCoders!")], [sg.Button("Click Me", button_color=("white", "green"), key="button")], ] window = sg.Window("My First PySimpleGUI Application", layout) while True: event, values = window.read() if event == "button": on_button_clicked() if event == sg.WIN_CLOSED: break window.close() |
In this example, we have first imported PySimpleGUI library and gives it an alias of sg. we then define a function called on_button_clicked that will be called when the button is clicked.
Next, we create layout for our window using list of lists. this layout consists of a text element and a button. after that we creates a window using the sg.Window constructor and pass in the layout as an argument.
In the while loop, we call the window.read method to wait for an event to occur. if the event is the button, we call the on_button_clicked function. if the event is the sg.WIN_CLOSED event, we break out of the loop and close the window.
Run the complete code and this will be the result.
It is just a basic window, now let’s add a label to the window.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
import PySimpleGUI as sg def on_button_clicked(): label.Update("Button was clicked!") layout = [ [sg.Text("Hello, GeeksCoders!")], [sg.Button("Click Me", button_color=("white", "green"), key="button")], [sg.Text("Label text goes here", key="label")], ] window = sg.Window("My First PySimpleGUI Application", layout) label = window["label"] while True: event, values = window.read() if event == "button": on_button_clicked() if event == sg.WIN_CLOSED: break window.close() |
In this example, we have added a label to the layout and given it a key of “label”. after that we retrieve reference to the label using the window[“label”] syntax. in the on_button_clicked function, we call the Update method of the label to change the text of the label to “Button was clicked!”. The rest of the code remains the same.
Run the complete code and this will be the result.
How to Add Layout in PySimpleGUI Window
In PySimpleGUI you can add layout to window by passing the layout list to sg.Window constructor. the layout list should consist of lists of elements that make up each row of the layout.
This is an example that demonstrates how to add a layout to window:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
import PySimpleGUI as sg layout = [ [sg.Text("Hello, GeeksCoders!")], [sg.Button("Click Me", button_color=("white", "green"))], ] window = sg.Window("My First PySimpleGUI Application", layout) while True: event, values = window.read() if event == sg.WIN_CLOSED: break window.close() |
In this example, the layout consists of two rows. the first row contains a text element, and the second row contains a button element. the sg.Window constructor takes the title of the window and the layout as arguments, and creates window with the specific layout. the rest of the code demonstrates how to read events and values from the window and how to close window when it’s no longer needed.
This is a basic example of calculator in PySimpleGUI.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
import PySimpleGUI as sg def evaluate_expression(expression): try: result = eval(expression) return str(result) except: return "Error" layout = [ [sg.Text("Calculator", font=("Helvetica", 20), text_color="blue")], [sg.Input(key="expression", size=(40, 1), font=("Helvetica", 15))], [sg.Button("Evaluate", key="Evaluate", font=("Helvetica", 15)), sg.Button("Clear", key="Clear", font=("Helvetica", 15))], [sg.Output(size=(40, 10), font=("Helvetica", 15), key="output")] ] window = sg.Window("Calculator", layout) while True: event, values = window.read() if event == "Evaluate": expression = values["expression"] result = evaluate_expression(expression) window["output"].Update(result) elif event == "Clear": window["expression"].Update("") window["output"].Update("") elif event == sg.WIN_CLOSED: break window.close() |
In this code, we have created layout with text element to display the title of the calculator, an input element to allow the user to enter an expression, two button elements to evaluate the expression and clear the input and output, and an output element to display the result of the expression. the evaluate_expression
function evaluates the expression and returns result as string. the rest of the code demonstrates how to read events and values from the window, update the elements in the window and close the window when it’s no longer needed, and if you run this code you will see a nice calculator with basic functionalities.
Run the complete code and this will be the result.
Is it Good to Use PySimpleGUI ?
Yes, PySimpleGUI can be good choice for creating GUI applications in Python. It offers simple, easy to use interface for creating graphical user interfaces, and it’s suitable for different range of applications.
One of the main advantages of PySimpleGUI is that it abstracts away the complexities of GUI programming, making it easy for developers to create GUI applications without having to deal with low level details. it means that you can focus on writing logic of your application, rather than on the details of GUI programming.
PySimpleGUI also offers different customization options, including the ability to change the appearance and behavior of elements, to add custom elements and to respond to events. this makes it possible to create different types of GUI applications, from simple utilities to complex multi window applications.
However, it’s important to note that PySimpleGUI is relatively new library, and it may not be as feature rich or as well documented as some of the more established GUI libraries for Python. in addition because it abstracts away the complexities of GUI programming, it may not be suitable for applications that require more advanced features, such as 3D graphics or custom drawing.
What are Other Options for Python GUI Development ?
There are several other options for Python GUI development, including:
- Tkinter: This is standard GUI library for Python and is included with most Python installations. it provides simple and easy to use interface for creating GUI applications and has been used for different types of applications, you don’t need to install this because it is already in Python installation.
- wxPython: This is open source GUI library for Python and it provides native look and feel on different platforms. it offers various features and customization options and it is the best option for different types of applications.
- PyGTK: This is GUI library for Python and it is based on the GTK+ GUI toolkit. it provides simple interface for creating GUI applications and it has been used for different types of applications.
- PyQt: This is GUI library for Python that is based on the Qt GUI toolkit. it provides numerous features and customization options and it is good for different types of applications including complex and multi window applications.
- Pygame: This is library for creating 2D games in Python. it provides simple and easy to use interface for creating games, and it has been used for different types of games from simple arcade-style games to complex action games.
Each of these libraries has its own strengths and weaknesses and the best choice for your application will depend on your specific needs and requirements. Tkinter for example may be good choice if you are looking for simple GUI library, while PyQt may be a good choice if you are looking for more feature rich library with large user community.
Final Thoughts
whether PySimpleGUI is a good choice for your application will depend on your specific needs and requirements. If you’re looking for a simple and easy-to-use GUI library for Python, and you don’t need advanced features, then PySimpleGUI may be a good choice for you.
-
Learn More on Python
- Get Started with wxPython: A Complete Guide to Building GUI Applications
- Python: The Most Versatile Programming Language of the 21st Century
- Tkinter: A Beginner’s Guide to Building GUI Applications in Python
- PySide6: The Cross-Platform GUI Framework for Python
- The Ultimate Guide to Kivy: Building Cross-Platform Apps with Python
- Discover the Power of Django: The Best Web Framework for Your Next Project
- How to Earn Money with Python
- Why Flask is the Ideal Micro-Web Framework
- Python Pillow: The Ultimate Guide to Image Processing with Python
- Get Started with Pygame: A Beginner’s Guide to Game Development with Python
- Python PyOpenGL: A Guide to High-Performance 3D Graphics in Python
- The Cross-Platform Game Development Library in Python
- Unleash the Power of Computer Vision with Python OpenCV
- PyQt6 Charts: An Overview and its Importance in Data Visualization