Skip to content
Home > Python for Newbies: Learn to Build a GUI Software

Python for Newbies: Learn to Build a GUI Software

    So far, in our sequence Python for Beginners , we mentioned many elements of Python such as lists , variables , tuples , resources , reading textual information content and much more.

    The only common denominator with the tutorials so far is that we are just building text-based purposes. These purposes are great and can be very useful (especially on machines that don't have a desktop atmosphere, otherwise you want them running in the background). You might even imagine that Python is not capable of building applications with graphical consumer interfaces (GUIs).

    He guesses? Your.

    These GUI applications are capable of utilizing problems such as:

    • radio buttons
    • verification list
    • menus
    • drop-down lists
    • wheels worth it
    • value bars
    • and extra

    With just a little help from a GUI toolkit, you can create GUI purposes with Python. Don't imagine me? Let me introduce you to how it is executed.

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

    Configure PySimpleGUI

    Before we create any GUI purposes with Python, we need to install a Python library that wraps multiple GUI libraries (such as Tkinter, Qt (pyside2), wxPython and Remi) in a single package to enable quick and easy GUI programming. Out of the field, PySimpleGUI defaults to Tkinter, however you can change it as desired.

    In other words, putting PySimpleGUI offers everything you need to start building GUI purposes with Python.

    To install PySimpleGUI, you will need a computer that already has Python installed. You will also need pip available for configuration. If you haven't added pip yet, do so with the command:

    sudo apt-get set up python3-pip -y

    With Pip inserted, you can now configure PySimpleGUI with the command:

    python3 -m pip set up pysimplegui

    At this level, you may be prepared to start building your first GUI utility.

    Hey, new battery!

    The main GUI utility we'll build is the ever-common Hey, World (with a New Stack twist). The first thing you should know is that the PySimpleGUI library must be imported into your utility. We've done this before, so it should be second nature.

    We will import PySimpleGUI into our utility as sg with the road:

    With this line, we will name any PySimpleGUI operation simply with sg .

    Our next line will outline the shape of a button for the device. This line will outline the textual content of the button as “Hey, New Stack” and the scale of the button as 30 x 4. This line looks like this:

    Discover the road makes use of a tuple (what's inside ()) and a listing (what's in []) to attract the button.

    Our next line pulls the exact window to the device and appears like this:

    What you see above is a variable (window) that uses the PySimpleGUI operation window to attract a graphical element called GUI SAMPLE with a measurement of 200 × 100.

    Finally, we use the .learn() function (which returns the required number of bytes from the file. The default is -1, which implies the entire file) to behave on a button click on the road:

    Our entire script (with feedback) looks like this:

    Save this file as hello_world.py . Run this system with the command:

    python3 hello_world.py

    What's best to see is a small window with a clickable button ( Determine 1 ).

    Python GUI

    Determine 1: Our Python GUI utility Hey, World.

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

    Accepting Consumer Login

    This time we will create a GUI that accepts consumer input. This time we will import the package Tkinter (which was put together with PySimpleGUI) to do the heavy lifting. Let's name this file input_gui.py . The primary line matters the Tkinter and it appears like this:

    Later, we will import the simpledialog from Tkinter with:

    Then we outline the base window with:

    With the base window outlined, we will then outline the sign-in dialog box (naming the window “Sign in, take a look”) and ask the consumer to enter their name with:

    Finally, we print the consumer enter again in the terminal window with the road:

    print("Hey", USER_INP)

    Our full utility looks like this:

    Save and close the file. Run the application with:

    python3 input_gui.py

    You will notice a small GUI window asking you to identify with OK and Cancel buttons ( Determine 2 ).

    Determine 2: Our GUI for consumer login.

    Rate your identity and click OK to see a message printed in the terminal window.

    And that's it... you've created your first GUI utility with Python. As you can see, this user-friendly language isn't just restricted to text-based apps. Python is actually a pretty flexible language, which helps make it much more useful.

    0