PySimpleGUI is a lightweight and easy-to-use Python library for creating graphical user interfaces (GUI) in a simple and straightforward manner. In this tutorial, I will guide you through the basics of PySimpleGUI and show you how to create your own GUI applications using this library.
Installation:
First, you need to install PySimpleGUI package using pip. Open your terminal and run the following command:
pip install PySimpleGUI
Creating a Simple GUI Window:
To create a simple GUI window using PySimpleGUI, you need to import the PySimpleGUI library and create a layout for the window. Here’s an example of a simple GUI window with a "Hello, World!" message:
import PySimpleGUI as sg
layout = [[sg.Text('Hello, World!')]]
window = sg.Window('My First GUI App', layout)
event, values = window.read()
window.close()
In this code snippet, we imported the PySimpleGUI library, created a layout with a Text element displaying "Hello, World!", created a window with the layout, and then read the events and values from the window before closing it.
Adding Elements to the GUI Window:
PySimpleGUI provides a variety of GUI elements that you can add to your window, such as Text, Input, Button, Checkbox, Radio, and Slider. Here’s an example of a GUI window with multiple elements:
import PySimpleGUI as sg
layout = [
[sg.Text('Enter your name:'), sg.InputText()],
[sg.Checkbox('Enable feature A'), sg.Checkbox('Enable feature B')],
[sg.Radio('Option 1', 'radio_group', default=True), sg.Radio('Option 2', 'radio_group')],
[sg.Slider(range=(1, 100), default_value=50, orientation='h')],
[sg.Button('Submit'), sg.Button('Cancel')]
]
window = sg.Window('My GUI App With Elements', layout)
while True:
event, values = window.read()
if event == sg.WIN_CLOSED or event == 'Cancel':
break
if event == 'Submit':
name = values[0]
feature_a_enabled = values[1]
feature_b_enabled = values[2]
selected_option = values[3]
slider_value = values[4]
# Process the input values here
window.close()
In this code snippet, we added various GUI elements to the layout, such as InputText, Checkbox, Radio, Slider, and Button. We then handled the events and values from the window by checking for the event type and extracting the input values.
Customizing the GUI Window:
You can customize the appearance of the GUI window by setting the theme and adding elements like menus, images, and tabs. Here’s an example of a customized GUI window with a menu, image, and tabs:
import PySimpleGUI as sg
# Define the layout with menu, image, and tabs
layout = [
[sg.Menu([['File', ['Open', 'Save', 'Exit']]]),
[sg.Image(filename='logo.png')],
[sg.TabGroup([[sg.Tab('Tab 1', [[sg.Text('This is Tab 1')]]),
sg.Tab('Tab 2', [[sg.Text('This is Tab 2')]])]])]
]
# Create the window with the layout
window = sg.Window('My Customized GUI App', layout)
while True:
event, values = window.read()
if event == sg.WIN_CLOSED or event == 'Exit':
break
window.close()
In this code snippet, we added a menu to the window with File options, an image element displaying a logo, and tabs with two tabs displaying text content. You can customize the appearance of the GUI window further by changing the colors, fonts, and sizes of the elements.
Conclusion:
In this tutorial, we covered the basics of PySimpleGUI and showed you how to create GUI applications with Python using this library. PySimpleGUI makes it easy to create simple and interactive GUI windows with a variety of elements and customization options. You can explore more advanced features of PySimpleGUI by referring to the official documentation and examples. Happy coding!
Drinking game: 1 shot when you hear PySimpleGui
this really sucks!!! I have learned python beyond these limits what offer this PySimpleGUI.
I have used Pysimplegui. It is simply a revolution compared to all the other GUI libraries, layout, and event loop processing is extremely simplified. In addition, there is also a web server based version, although I have not tested it.
'gooey' 'ping'
I'm new to coding.
Is that "simple"???
I mean, i just saw a 15 min video of coding!!
It is so difficult to create a compiles with a drag and drop to window UI?
Years ago, i used Visual studio and had exactly that.
You could design a windows app just by dragging and dropping buttons and styles into a window.
Later by clicking them, of course you programming them to do whatever you wanted !!
How to make this PysimpleGUI into an App with shortcut?
Looks dated
32 years old and still, Python does not have a GUI builder like Visual BASIC. Instead you are expected to write code to create GUI elements. Ridiculous.
how do i put a transparent word in the text box?
Does anyone know how to make a scrolling rect object for pygame with PySimpleGUI. This would hold all the buttons for buildings and their prices for a clicker game
Everything still looks like it was made in 1999, disappointing
Very nice, thanks
why learn another syntax to achieve the same goal. we need graphics auto converted to tkinter code like Pygubu does.
Very concise with lots of useful examples, step by step presented. Thanks.
in your code you have no with " sg.Column(file_list_column), " but i do
it says its not defined
I thought this tut was good until i notaced that id doesn't work in py charm i did the same but it said that a list can only contain intigers and slices!
Thank you, Good Gui App.
Ah, a worrying trend. Promise more and then, nothing. Does no encourage one to subscribe. Perhaps PySimpleGUI is actually garbage. Hard to tell. Came looking as tKinter is a pain in the butt.
Yes, keep spreading python, keeps me busy replacing all the pyhton code. Companies are abandoning puthon at an astounding rate. After some kid comes in and scripts something they hire people like me to build it correctly and remove all the python.
Nope. Need a RAD IDE with a form designer like Visual Studio for NET or Delphi, otherwise it's just a lot of boilerplate typing instead of getting on and programming your application logic.