Simple GUI with PySimpleGUI

Posted by


PySimpleGUI is a Python package that allows developers to create simple and easy-to-use GUI applications quickly and efficiently. It is a wrapper around tkinter, Qt, WxPython, and Qt for Python that simplifies the process of building GUIs for Python applications. In this tutorial, we will cover the basics of PySimpleGUI and guide you through the process of creating your first GUI application.

Installation
To get started with PySimpleGUI, you first need to install the package. You can install it using pip by running the following command:

pip install PySimpleGUI

Once the package is installed, you can import it into your Python script using the following statement:

import PySimpleGUI as sg

Creating a Simple GUI Window
To create a simple GUI window using PySimpleGUI, you can use the sg.Window() class. Below is an example of a basic GUI window with a title and layout:

import PySimpleGUI as sg

layout = [[sg.Text("Hello, PySimpleGUI!")], [sg.Button("OK")]]

window = sg.Window("My First GUI App", layout)

event, values = window.read()

window.close()

In this code snippet, we first define the layout of the GUI window using a list of GUI elements. In this case, we have a text element that displays the message "Hello, PySimpleGUI!" and a button element labeled "OK".

We then create a PySimpleGUI window object by passing the window title and layout as arguments to the sg.Window() class. The window.read() method is used to capture user events and input values. Finally, we close the window using the window.close() method.

Adding Event Handling
The window.read() method returns a tuple containing the event that triggered the function call and the input values from the GUI elements. You can add event handling logic to respond to user interactions with the GUI elements. Below is an updated version of the previous example with event handling added:

import PySimpleGUI as sg

layout = [[sg.Text("Hello, PySimpleGUI!")], [sg.Button("OK")]]

window = sg.Window("My First GUI App", layout)

while True:
    event, values = window.read()

    if event == sg.WIN_CLOSED or event == "OK":
        break

window.close()

In this updated code snippet, we use a while loop to continuously read user events from the GUI window. If the user clicks the "OK" button or closes the window, the loop breaks, and the window is closed using the window.close() method.

Adding Input Elements
PySimpleGUI provides various input elements such as text boxes, input fields, checkboxes, radio buttons, drop-down menus, sliders, and file browsers. You can combine these elements with text labels and buttons to create interactive GUI applications.

import PySimpleGUI as sg

layout = [[sg.Text("Enter your name:"), sg.InputText()],
          [sg.Button("Submit")]]

window = sg.Window("User Input", layout)

while True:
    event, values = window.read()

    if event == sg.WIN_CLOSED or event == "Submit":
        break

    user_name = values[0]
    sg.popup(f"Hello, {user_name}!")

window.close()

In this example, we have added a text input field where the user can enter their name. When the user clicks the "Submit" button, a popup message is displayed with the greeting "Hello, {user_name}!".

Customizing the GUI Layout
PySimpleGUI offers a flexible and easy-to-use layout design that allows you to customize the appearance and functionality of your GUI application. You can specify the size, color, font, and alignment of GUI elements in the layout using various properties and options.

import PySimpleGUI as sg

layout = [[sg.Text("Customizing GUI Layout", font=("Arial", 20), text_color="blue")],
          [sg.Text("Enter your name:", size=(15, 1)), sg.InputText(size=(20, 1))],
          [sg.Button("Submit", button_color=("white", "green"), size=(10, 1))]]

window = sg.Window("Custom GUI Layout", layout)

while True:
    event, values = window.read()

    if event == sg.WIN_CLOSED or event == "Submit":
        break

    user_name = values[0]
    sg.popup(f"Hello, {user_name}!", font=("Arial", 16), text_color="green")

window.close()

In this customized layout example, we have specified the font, text color, size, and button color of the GUI elements to create a visually appealing interface. You can experiment with different layout properties and options to create a unique and user-friendly GUI design.

Conclusion
PySimpleGUI is a powerful and intuitive Python package for building GUI applications with ease. In this tutorial, we covered the basics of PySimpleGUI and demonstrated how to create simple GUI windows, add event handling, input elements, and customize the layout of a GUI application. With its simple syntax and rich set of features, PySimpleGUI is an excellent choice for developers looking to create interactive and user-friendly GUI applications in Python.

0 0 votes
Article Rating

Leave a Reply

1 Comment
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
@AsimCode
2 hours ago

Please subscribe to support Asim Code!

https://www.youtube.com/channel/UC2wyJKxwEEk_CK_HkqgZ_6g?sub_confirmation=1

1
0
Would love your thoughts, please comment.x
()
x