So far, in our Python for Beginners series, we’ve discussed many aspects of Python, such as lists, variables, tuples, functions, reading text from files, and so much more.

The one common denominator with the tutorials so far is that we’re only building text-based applications. Those applications are great and can come in very handy (especially on machines that don’t include a desktop environment or you need them to work in the background). You might even believe that Python isn’t capable of building apps with graphical user interfaces (GUIs).

Guess what? It is.

These GUI apps are capable of using things like:

  • radio buttons
  • checklists
  • menus
  • dropdown lists
  • value wheels
  • value bars
  • and more

With just a little help from a GUI toolkit, you can create GUI applications with Python. Don’t believe me? Let me show you how it’s done.

I’ll be demonstrating on Pop!_OS Linux, but this can be done with any distribution that supports Python.

Install PySimpleGUI

Before we can create any GUI applications with Python, we have to install a Python library that wraps various GUI libraries (such as Tkinter, Qt (pyside2), wxPython, and Remi) in a single package to allow for fast and simple GUI programming. Out of the box, PySimpleGUI defaults to Tkinter, but you can switch it up as needed.

Read More:   Update Caching out of Hadoop: How New York Times Embraces New Technology

In other words, installing PySimpleGUI gives you everything needed to start building GUI applications with Python.

To install PySimpleGUI, you’ll need a machine that already has Python installed. You’ll also need pip available for the installation. If you’ve not already installed pip, do so with the command:

sudo apt-get install python3-pip -y

With Pip installed, you can now install PySimpleGUI with the command:

python3 -m pip install pysimplegui

At this point, you are ready to start building your first GUI application.

Hello, New Stack!

The first GUI application we’ll build is the always popular Hello, World (with a New Stack twist). The first thing you must know is that the PySimpleGUI library must be imported into your application. We’ve done this before, so it should be second nature.

We’ll import PySimpleGUI into our application as sg with the line:

With that line, we can call any PySimpleGUI function with just sg.

Next, we’ll create the window, along with a title name and some dimensions. This line looks like this:

Our next line will define the layout of a button for the application. This line will define the text for the button as “Hello, New Stack” and the size of the button as 30 x 4. This line looks like this:

Notice the line uses a tuple (what’s within ()) and a list (what’s within []) to draw the button.

Our next line draws the actual window for the application and looks like this:

What you see above is a variable (window) that uses the PySimpleGUI function Window to draw a graphical element labeled GUI SAMPLE with a size 200×100.

Read More:   Why an AppOps Model Empowers Developers – InApps 2022

Finally, we use the .read() function (which returns the specified number of bytes from the file. the Default is -1 which means the whole file) to act on a button click with the line:

Our whole script (with comments) looks like this:

Save that file as hello_world.py. Run the program with the command:

python3 hello_world.py

What you should then see is a small window with a clickable button (Figure 1).

Python GUI

Figure 1: Our GUI Python Hello, World application.

Click the Hello, New Stack button and the window will close.

Accepting User Input

This time around, we’ll create a GUI that accepts user input. This time we’ll import Tkinter package (which was installed with PySimpleGUI) to do the heavy lifting. Let’s call this file input_gui.py. The first line imports Tkinter and looks like this:

Next, we’re going to import simpledialog from Tkinter with:

We then define the root window with:

With the root window defined, we can then define the input dialog (naming the window “Input Test”) and prompt the user to type their name with:

Finally, we print out the user input back in the terminal window with the line:

Read More:   Navigating the Human Element of a DevOps Transformation – InApps 2022

print("Hello", USER_INP)

Our entire application looks like this:

Save and close the file. Run the app with:

python3 input_gui.py

You will see a small GUI window asking for your name with OK and Cancel buttons (Figure 2).

Figure 2: Our GUI for user input.

Type your name and click OK to see a message printed in the terminal window.

And there you go… you’ve created your first GUI application with Python. As you can see, this user-friendly language isn’t only limited to text-based apps. Python is, in fact, quite a versatile language, which helps to make it even more useful.