Introduction to PySimpleGUI

Posted by


PySimpleGUI is a Python GUI framework that aims to make creating simple and easy-to-use graphical user interfaces an effortless process. It is built on top of the Tkinter library, which is the standard GUI toolkit for Python.

In this tutorial, we will cover the basics of getting started with PySimpleGUI, including installation, creating a simple GUI window, adding widgets, and handling events.

Installation:
To install PySimpleGUI, you can use pip, the standard package manager for Python. Simply open a terminal and run the following command:

pip install PySimpleGUI

Creating a simple GUI window:
To create a simple GUI window using PySimpleGUI, you can use the sg.Window class. Here is an example code snippet that creates a simple window with a title and size:

import PySimpleGUI as sg

# Create a simple window
window = sg.Window('My First GUI', size=(300, 200))

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

    if event == sg.WINDOW_CLOSED:
        break

window.close()

This code creates a simple GUI window with the title "My First GUI" and size 300×200 pixels. The WINDOW_CLOSED event is used to close the window when the user closes it.

Adding widgets:
PySimpleGUI provides a variety of widgets that you can add to your GUI window, such as buttons, input fields, text labels, and more. Here is an example code snippet that adds a button and a text label to the window:

import PySimpleGUI as sg

# Create a simple window with a button and a text label
layout = [
    [sg.Button('Click me')],
    [sg.Text('Hello, World!')]
]

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

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

    if event == sg.WINDOW_CLOSED:
        break

window.close()

This code creates a window with a "Click me" button and a text label that displays "Hello, World!". You can customize the layout and appearance of the widgets by changing the properties of each widget in the layout list.

Handling events:
In PySimpleGUI, you can handle events like button clicks, text input, and window close events by checking the value of the event variable in the event loop. Here is an example code snippet that handles a button click event:

import PySimpleGUI as sg

# Create a simple window with a button
layout = [
    [sg.Button('Click me')]
]

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

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

    if event == sg.WINDOW_CLOSED:
        break
    elif event == 'Click me':
        sg.popup('Button clicked!')

window.close()

This code displays a popup message when the "Click me" button is clicked. You can handle different events by checking the value of the event variable and performing different actions based on the event type.

In this tutorial, we covered the basics of getting started with PySimpleGUI, including installation, creating a simple GUI window, adding widgets, and handling events. PySimpleGUI provides a simple and intuitive way to create graphical user interfaces in Python, making it an excellent choice for beginners and experienced developers alike.

0 0 votes
Article Rating
6 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
@Кирэ-Ни
1 month ago

Вот у меня, видимо, проблемы с чтением документации – начала с этой библиотеки, и тут, наоборот, столкнулась с болью при её понимании((

@hexruin4569
1 month ago

Нашел данный канал по запросу "PySimpleGUI". А вот плейлист бы скомпоновать в обратном порядке ;D

@GameChannelOfficial
1 month ago

Наес

@volodymyr8649
1 month ago

Вау :3

@citieslg
1 month ago

Успехи очень важны)

@k0ccc
1 month ago

Будет 5ти минутный гайд?)