Developing applications very frequently involves providing an interface to the user for interaction, input values, select options and view the output. This interface is mostly presented as a graphical User Interface (GUI). If you have been wondering that whether you can create such interfaces in your Python programs, then you are right in your thoughts. PySimpleGUI is the Python package to add the GUI touch in your apps where you need to get data from user to proceed with your application. In this PySimpleGUI tutorial we will learn about how to create impressive GUIs for a variety of applications.
Installing PySimpleGUI in Windows, Linux or Mac
The first step of course would be to install PySimpleGUI package in your system. You will install the package with pip or pip3 command depending upon your system.
pip install --upgrade PySimpleGUI
or
pip3 install --upgrade PySimpleGUI
Using PySimpleGUI in your Program
Just like your other Python programs, you need to import PySimpleGUI before using any of its capabilities. It is done by writing import statement at the beginning of the program.
import PySimpleGUI as psg
Creating First PySimpleGUI program
Use the following steps to create a PySimpleGUI program.
Now after these simple startup activities we have reached the stage to write our first PySimpleGUI program. “Hello World” is the traditional way to go. But we will do it a bit differently this time. Let’s ask the user name and say hello to the user.
In the PySimpleGUI program you need to define the layout of the window to display the interface elements like text, input boxes, buttons, checkboxes, dropdowns etc. The layout is defined as a Python list of the elements of the GUI interface.
lt=[ list of GUI elements]
Once you define the layout it is passed to the window object of the PySimpleGUI package.
win= psg.window(“Your Application”, lt)
You can change the title of the window. Now to capture the actions of the user and react to it you need to read the window. This is done by using the events (e) and values (v) pair as shown-
e,v=win.read()
you can use any names for the events and values pair (We have used e and v here. Same goes with window and layout)
Yes, you have guessed right e is an array that captures all the events and v is the array storing the values user has entered or chosen in the interface. So to display the greeting to the user the v[0] value is used and displayed on the command window as the output. You can use the popups you know so well, but it will be a bit later after making you acquainted with some interface elements by PySimpleGUI tutorial .
import PySimpleGUI as psg #add required elements in layout lt = [[psg.Text("Name")],[psg.Input()], [psg.Button("Message")] ] #define window with title and layout win = psg.Window('Greetings', lt) #present interface to user to get user input and events e, v = win.read() #diaplay output in command window of Python using print print('Hello', v[0], "! Welcome to CSVeda.com") #Cleanup window win.close()
Output
Read more about PySimpleGUI . Read about installing Python in Windows
PySimpleGUI Topics
- Themes, Text, Input, Multiline and Button
- Radio Button and Checkbox
- Combo and Listbox
- Menu, Button Menu and Option Menu
- Progress Bar, Spin and Slider
- Column and Frame
Be First to Comment