As at the time of writing this (February 14th 2020), the latest version of Python is version 3.8.1. I guess any version of Python 3 will work. Python 2 is great but won’t work if you’re using PyQt5.

Go to:
https://www.riverbankcomputing.com/software/pyqt/download5
download and install the latest version of PyQt appropriate to your operating system. It would be a good idea to make the binaries visible on your system path.

If you want to follow along, you can create a new text file and paste in the following code. Else, this and all other code will be found in the source code that came with your book. The code for each chapter is in a folder named after that chapter.

First example

In the folder for this chapter, open a file named simple.py.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
from PyQt5.QtWidgets import QApplication, QWidget // (1)
import sys // (1)

class MainWindow(QWidget): // (6)
    def __init__(self): // (7)
        super().__init__() // (8)
        self.initUI() // (9)

    def initUI(self):
        self.setWindowTitle('Simple example 1') // (10)
        self.resize(230, 254) // (11)
        self.show() // (12)


if __name__ == '__main__':    // (2)
    qApp = QApplication(sys.argv) // (3)
    w = MainWindow() // (4)
    sys.exit(qApp.exec_()) // (5)
  1. These lines import the modules needed to run the script. The function exit() that we’ll later use to terminate our script is part of the sys module.

  2. When the Python interpreter reads a source file, it executes all the code found in it.
    Before executing the code, it will define a few special variables. For example, if the Python interpreter is running the source file as the main program, it sets the special __name__ variable to have the value "__main__". If this file is being imported from another module, __name__ will be set to the module’s name.
    In the case of our script, let’s assume that it’s executing as the main function, ie. you type something like
    python simple.py
    on the command line. After setting up the special variables, it will execute the import statement and load the modules specified in lines 1 and 2. It will then read the if statement and see that __name__ does equal "__main__", so it will execute the block shown there. Line 15 allows us to intercept the interpreter execution flow and harness it for our ends.

  3. Every PyQt application must contain an instance of the application object. The sys.argv parameter contains a list of arguments passed to the script from the command line. In our case, we didn’t pass any arguments to our script.

  4. We instantiate an object called w from the MainWindow class. The MainWindow class contains all the functionality of our simple app.

  5. We enter the main loop of the application. In programs with UI( user interface) components, whatever happens in the program for example if we resize the main window, drag objects into the main window is determined by actions the user makes.
    Line 18 ensures that the execution flow loops infinitely while “listening” for user input. The event we’re most interested in is the exit event, that is when the user closes the main window. We pass this to our script as its cue to terminate our script.
    You may have noticed that our exec()_ method has an underscore. This is because exec is a reserved Python keyword which we can’t use.
    So far we have looked at the script initialization boilerplate of our code. Now we examine the most important part of our simple script.

  6. Here we define our main class which inherits from the QWidget class. This is why QWidget is passed as an argument to our class.

  7. We called the init() method for our derived class on line 5. super() returns the parent object of our base class. Once we have this object, we call its init() method. This allows PyQt to perform its default initialization on our widgets.

  8. We call the initUI() method. All our UI initialization is done in this method.

  9. We declare the initUI() method.

  10. Our widget in this example is our main window. We set the window title by calling the setWindowTitle() method and passing it the text we want to use as an argument.

  11. The resize() method simply sets the size of a widget. We set a size of 230 pixels width and 254 pixels height.

  12. We display our widget on screen. All UI components we interact with in PyQt for example buttons, sliders, drop-down menus etc are called widgets. The base class for all UI objects in PyQt is called QWidget.

Tip

The syntax for a derived class definition in Python looks like this:

class DerivedClassName(BaseClassName):
   <statement 1>
   ...
   <statement n>

To run the program, switch to the directory where the script is contained in your CLI. Type in the command:
python simple.py

Your output should look like this:

image1
Figure 1.1 Your first PyQt app

Second example

Look at the top-left hand corner of the illustration above! This is the generic icon which shows up in scripts run on Windows. You’ll learn how to set up your own icon in the following script.

Open the file named simple1.py. Most of the code in this example was covered in the previous example. Let’s go through what’s new together.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
from PyQt5.QtWidgets import QApplication, QWidget, QPushButton
from PyQt5.QtGui import QIcon
import sys

class MainWindow(QWidget):
    def __init__(self):
        super().__init__()
        self.initUI()

    def initUI(self):
        self.setWindowTitle('Simple example 2')

        self.setWindowIcon(QIcon('240px-Smiley.svg.png')) // (1)

        btnClick = QPushButton(parent=self) // (2)
        #btnClick = QPushButton(self)
        btnClick.setText('Quit') // (3)
        btnClick.move(110, 50) // (4)
        btnClick.clicked.connect(QApplication.instance().quit) // (5)

        self.resize(300, 254)
        self.show()


if __name__ == '__main__':
    qApp = QApplication(sys.argv)
    w = MainWindow()
    sys.exit(qApp.exec_())
  1. We set an icon for our application. The setWindowIcon() method accepts a QIcon object as its argument. Of course, the file specified must exist or else Qt will fall back to the generic icon for your operating system.

  2. Here we create a button widget.
    It’s important to note that a widget without a parent widget is always an independent window, called a top-level widget. In the previous example, we worked exclusively with a top-level widget. Methods like setWindowTitle() and setWindowIcon() are only available to top-level windows.
    Non-window widgets are child widgets, displayed within their parent widgets. Most widgets in Qt are mainly useful as child widgets. For example, it is possible to display a button as a top-level window, but most people prefer to put their buttons inside other widgets like we’re doing on line 15.
    You can declare a child widget like we have here on line 15 or you can do it as shown on line 16.
    Please note that if you don’t specify a parent for this widget, then it won’t be visible on screen!

  3. We set the text property of our button. This is the text that will be seen on the surface of our button.

  4. We position our button relative to our parent widget.

  5. Here we set up code to handle user clicks on the button.
    Android, Swift and many other “visual” programming languages, have callback functions to handle input events. Qt uses a signal and slot mechanism to handle input events.
    In our case, a signal is emitted when the user clicks on the button we’ve created. A slot is code that processes this signal. A slot can point to a Python function or another slot.
    The emitted signal is connected to the infinite loop which processes user events. The signal is connected to the quit() method which terminates the application. Communication is done between two objects: the sender and the receiver. The sender is the push button, the receiver is the application object.

To run the program, switch to the directory where the script is contained in your CLI. Type in the command:
python simple1.py

Your output should look like this:

image2
Figure 1.2 Second example

Summary

This chapter walked you through installing PyQt as well as the basics of some very simple PyQt apps.

There is already a problem though: run simple2.py again to see it. The app as shown in Figure 1.2 has the button positioned roughly at the horizontal center of the screen. When you maximize the parent window, the button is pushed towards the top-left of the parent window. Try it for yourself and confirm this! The next chapter will teach you how to preserve widget positions regardless of parent window size.

References