PySimpleGUI is a simple yet powerful GUI library for Python that allows you to easily create graphical user interfaces using a minimal amount of code. In this tutorial, we will cover the basics of PySimpleGUI and show you how to create your first GUI application.
Installation:
First, you need to install the PySimpleGUI library using pip. You can do this by running the following command in your terminal:
pip install PySimpleGUI
Creating a basic window:
To create a basic window using PySimpleGUI, you need to import the library and use the sg.Window
class. Here’s a simple example that creates a window with a title and a button:
import PySimpleGUI as sg
layout = [
[sg.Text('Hello World!')],
[sg.Button('Click me')]
]
window = sg.Window('My First GUI App', layout)
while True:
event, values = window.read()
if event == sg.WIN_CLOSED:
break
window.close()
In this code snippet, we import the PySimpleGUI library and define the layout of the window using a list of widgets (in this case, a text label and a button). We then create an instance of the sg.Window
class with the title ‘My First GUI App’ and the specified layout. Finally, we enter a loop where we constantly check for user events (e.g., button clicks) using the window.read()
method.
Handling events:
PySimpleGUI uses an event-driven model, meaning that you need to define event handlers for user actions (e.g., button clicks). In the previous example, we checked for events in the while loop, but we didn’t handle any specific actions. Let’s modify the code to display a message when the button is clicked:
import PySimpleGUI as sg
layout = [
[sg.Text('Hello World!')],
[sg.Button('Click me')]
]
window = sg.Window('My First GUI App', layout)
while True:
event, values = window.read()
if event == sg.WIN_CLOSED:
break
if event == 'Click me':
sg.popup('You clicked the button!')
window.close()
In this updated code, we added an additional if
statement to check if the event corresponds to the button being clicked. If so, we display a popup message using the sg.popup()
method.
Adding more widgets:
PySimpleGUI provides a wide range of built-in widgets that you can use to create more complex GUI layouts. Here are some common widgets you can use:
- Text labels:
sg.Text
- Input fields:
sg.Input
- Buttons:
sg.Button
- Checkboxes:
sg.Checkbox
- Radio buttons:
sg.Radio
- Combo boxes:
sg.Combo
You can find the full list of available widgets in the PySimpleGUI documentation.
Customizing the layout:
You can customize the appearance of your GUI application by adjusting the layout and styling of the widgets. PySimpleGUI provides various options for styling, such as changing the font, colors, and sizing of widgets:
import PySimpleGUI as sg
layout = [
[sg.Text('Enter your name:', font=('Arial', 14))],
[sg.Input(size=(20, 1), font=('Arial', 12))],
[sg.Button('Submit', button_color=('white', 'green'), size=(10, 1))]
]
window = sg.Window('Customized GUI App', layout)
while True:
event, values = window.read()
if event == sg.WIN_CLOSED:
break
window.close()
In this example, we added custom styling to the text label, input field, and button widgets by specifying font sizes, colors, and sizes.
Conclusion:
PySimpleGUI is a versatile and user-friendly library that makes it easy to create GUI applications in Python. In this tutorial, we covered the basics of creating a simple GUI application, handling user events, adding widgets, and customizing the layout. With PySimpleGUI, you can quickly build interactive applications for a wide range of use cases. I hope this tutorial has given you a good introduction to PySimpleGUI and inspired you to create your own GUI applications.
in the next time try to make some machine learning projects
Fantastic video