PyQt5 Beginner tutorial

(Last Updated On: November 20, 2018)


This is a PyQt5 beginner tutorial. It will help you get up and running with PyQt in the shortest possible time. A basic knowledge of Python is assumed.

PyQt is a Python binding of the cross-platform GUI toolkit Qt.It is one of Python’s options for GUI programming. Other alternatives include PySide, PyGTK, wxPython, and Tkinter.

PyQt is best suited for development of non-commercial applications(GPL licence). If you want to develop commercail applications, PySide is quite plopular and is released under the LGPL licence.

Installing PyQt

You will need the latest version of Python(currently 3.3.3). Make sure this is installed and that you add the executable to the system path. This option is automatically available from within the installer.

Once that is done, download an appropriate binary from the Riverbank website and choose the default installation options.

Writing your first script

Fairly simple no? Lines 1-2 define the necessary modules needed.

Line 4: QWidget is the base class of all user interface objects in PyQt5, so you are creating a new Form class that inherits from the base class, QWidget.

Lines 5-6: The default constructor for QWidget. The default constructor has no parent, and a widget with no parent is known as a window.

Lines 7-9: Here we add a label, a text edit box and a submit button.

Lines 12-15: We add a QVBoxLayout.  QVBoxLayout class lines up our widgets vertically.

Line 17: This adds an event handler, the function submitContact() for our submit button.

Lines 19-21: We add a QGridLayout. QGridLayout lays the widgets out in a grid. Widgets can be positioned as shown using Cartesian co-ordinates.

Lines 23-24: Set the QGridLayout as the window’s main layout. After that we set it’s title.

Line 27: We get the text contain in the widget nameLine.

Lines 29-35: If nameLine contains no text we issue an alert. If there is some text in the variable, we issue another alert, this time including the string entered by the user.

The rest of the code should be easy to follow. We create an instance of the Form object called screen. The show() method will display the widget on screen.

We then begin the event handling loop for the application. The event handling loop waits for an event to occur and then dispatches it to perform some task. The event-handling loop continues to work until either the exit() method is called or the main widget is destroyed. The sys.exit() method ensures a clean exit, releasing memory resources.

To run the script simply invoke

python <name of script>

To give you this:


This was a very basic tutorial. A comprehensive reference can be found here.

** Update, March 26th 2018: Click here to recieve your PyQt5 book **

5 Replies to “PyQt5 Beginner tutorial”

  1. Hi 🙂
    Well – it is a simple tutorial – but for a beginner of PyQt5 I still need to know how to come from a GUI layout to the Python programmin. It’s an exstreamly simple issue to solve, but it’s still an issue weeks after the release of PyQt5. What about an update og the Help inside PyQt5?

    Kind regards

  2. Pingback: PyQt5 Beginner tutorial | [email protected] |
  3. just fyi, on Windows 7, I removed the “amp;” in front of “Submit” and that worked better. thanks for the sample.

  4. There is an error in your script

    QMessageBox.information(self, “Success!”,
    “Hello “%s”!” % name)

    should be:
    QMessageBox.information(self, “Success!”,
    “Hello %s!” % name)

Leave a Reply

Your email address will not be published. Required fields are marked *

Reload Image