Posted on by & filed under Content - Highlights and Reviews, Programming & Development.

A guest post by Warren and Carter Sande, the authors of Hello World! Second Edition: Computer Programming for Kids and Other Beginners. Warren Sande is an electronic systems engineer who uses Python both as a “do anything” scripting language and as a way to teach computers and programming. Carter Sande is a high school student who is passionate about technology. When he’s not fixing his school’s network and helping his classmates recover lost homework, he likes to ride his bike and write retro video games.

One thing most people need at some point when writing Python for desktop applications is a way to make Graphical User Interfaces. Qt is a GUI framework, available as free and open source software. PyQt is the Python wrapper for Qt. Our two main criteria for a GUI framework are:

  • Easy to use, particularly for beginners
  • Cross-Platform (Windows, Linux, Mac)

PyQt meets the first criteria pretty well, and it definitely meets the second criteria. Although there is a lot of power and depth to PyQt, it’s pretty easy to do simple things with it. In this respect, it follows the Python philosophy of making simple things simple and complex things possible. In this post, we’ll use PyQt to build the Temperature conversion GUI from Chapter 20 of Hello World! Second Edition: Computer Programming for Kids and Other Beginners.

Basic workflow

One of the things that a GUI development tool needs in order to be easy to use for beginners is a drag-and drop GUI builder. This is where you drag widgets (buttons, text boxes, etc.) onto your window, resize them, and change properties, and the GUI builder generates some code for you. Qt includes something called “Qt Designer,” which is a drag-and drop GUI builder. It looks like this:


You drag a widget onto the window, resize it, and change its properties in the property editor. When you’re done, you save the GUI as a .ui file. Then you create a Python file that contains the “application code” – program logic and event handlers for the GUI widgets. Both the GUI code and the application code require the PyQt library. Let’s look at the process in more detail.


The Hello World installer installs PyQt for you, so that’s the easiest way to do it, and it’s what we recommend.

Starting a new GUI

To start a new GUI, open up Qt Designer. When you start Qt Designer, you get a New Form dialog. We pick Main Window, since we are developing a complete (although simple) application, and click the Create button. This brings up a blank, grey window where you can place widgets. On the left is the “WidgetBox.” From there, you can drag widgets onto the window. We placed a couple of Push Buttons, a couple of Labels, a Line Edit and a Spin Box. When we’re done, the GUI looks like this:


Once the widgets are in the GUI, you can move them around (by dragging them) and resize them (by dragging an edge or corner). Whenever you select one, its properties are shown in the Property Editor on the right. The first property in the list is the objectName, which is used to refer to the object in code. We changed these to make them more meaningful: btn_CtoF, btn_FtoC, spinFahr, and editCel. (We didn’t bother renaming the label widgets, since you never need to refer to those in the code.) We save this as tempconv.ui. You can look at the .ui code in any text editor to see what it looks like. It is just an XML file that describes the locations and properties of the various GUI widgets. Now we need to write some code to make this GUI work. This code will have event handlers for the buttons, so that when you click the buttons, it will do the temperature conversion and display the results. In IDLE, we make a new file,, which looks like this:

At the top, we have to import the PyQt library and load the .ui file that we just generated using QtDesigner. There is one class, which is the main window of the GUI. The class has an __init__() function, which sets up the GUI. In the __init__() function, we also have to “connect” the events to the event handlers. That’s done in these 2 lines:

The connect() function is what we use to connect events (or “signals”, as Qt calls them) to event handlers. It takes a single parameter, which is the name of the event handler function. Note here that:

  • btn_CtoF is the name of the button
  • The button has a clicked.connect() method, which defines what event handler function is called when the button is clicked
  • The name of the event handler function is btn_CtoF_clicked

Then, we need to create the event handlers themselves, with names that match what we put in the connect() function. The event handlers are also defined within the GUI class. For example:

This gets the Celsius value from the text edit widget (editCel), converts it to Fahrenheit, and puts that value in the Fahrenheit box (spinFahr). In the __init__() function, we defined the main window as self, so we reference the widgets as self.spinFahr (for example). Then at the end of the file, we need some code to start the GUI running. That’s these lines:

That’s all there is to a basic GUI using PyQt. If you have followed along, you can try running it to make sure it works. You’ll probably notice that the spinBox has a default min and max value (0 and 99), so you probably want to set these to a wider range. PyQt also allows you to add things like menus and toolbars to your GUIs. To see this example in more detail, we invite you to read Hello World! Second Edition: Computer Programming for Kids and Other Beginners, available now on Safari. For more details about Python, see the Safari resources referenced below.

Safari has the content you need

Hello World! Second Edition: Computer Programming for Kids and Other Beginners introduces the world of computer programming using Python in a clear and engaging style. Written by Warren Sande and his son Carter, it is full of examples that will get you thinking and learning. Reviewed by professional educators, this book is kid-tested and parent-approved. You don’t need to know anything about programming to use the book, just the basics of using a computer. If you can start a program and save a file, you’ll be off and running!
Learning Python, 5th Edition will help you quickly write efficient, high-quality code with Python. It’s an ideal way to begin, whether you’re new to programming or a professional developer versed in other languages.
Hello! Python fully covers the building blocks of Python programming and gives you a gentle introduction to more advanced topics such as object-oriented programming, functional programming, network programming, and program design. New (or nearly new) programmers will learn most of what they need to know to start using Python immediately.

Tags: Beginner, Carter Sande, cross-platform, GUI, Hello World, PyQT, Python, Warren Sande,

One Response to “Create a Basic GUI Using PyQt”

  1. Markus R.

    “Qt is a GUI framework now owned by Nokia” ???

    Qt got sold to Digia quite a while ago. And Nokia doesn’t exist anymore.