Create a Simple GUI using Python and PySimpleGUI


 

Create a Simple GUI using Python and PySimpleGUI

Today, We are working on with a new python GUI package, which is simple and understandable. It was developed to ease the GUI development. You are not required to write many lines of code to build up a beautiful GUI. PySimpleGUI is  a wrapper for tkinter and PyQT. Web based applications can be developed easily using PySimpleGUI. 

Installing PySimpleGUI:

pip install pysimplegui
pip3 install pysimplegui

We can also use pip along with python to install packages:

python -m pip install pysimplegui

After finishing up all the steps above, now, let's get into the process of creating our first GUI application.
Firstly, we need to import the PySimpleGUI package:

import PySimpleGUI as sg

Now, we have imported our GUI package and provided an alias for the package as sg. Next, we need to write a layout.
Layout is the building block of our GUI, it consists of rows and columns.

layout=[[sg.Text("Hello")]]

Observe the syntax of layout, we have created a list and inside that list, we have created another list. So, a layout can contain any number of lists in it. These lists will be treated as rows and columns. In the above code block we have created  row and inside that row, we have inserted a Label or Text "Hello".
Now, we need to create our GUI window inorder to place the Text on it. 

my_new_window=sg.Window('My New Window',layout)

Congrats! now we have created a window and placed our layout on it. So what's next?  Grab the events and values triggered from the window and it's components.

events,values=my_new_window.Read()

The Read() method will grab all the events and values from the elements and store it inside events, values respectively.
We will be looking at how to handle the events and values later in our blog.

Complete Code

import PySimpleGUI as sg

layout=[[sg.Text('Hello')]]

my_new_window=sg.Window('My New Window',layout)
events,values=my_new_window.Read()

Thank You.

Post a Comment

Previous Post Next Post