PyQt is a library that lets you use the Qt GUI framework from Python. Qt itself is written in C. By using it from Python, you can build applications much more quickly while not sacrificing much of the speed of C. PyQt5 refers to the most recent version 5 of Qt. Create the python code for hw.ui by using the following command in a command window (not in python or ipython interpreters): pyuic4 -x hw.ui -o hw.py This command creates a file 'hw.py' which contains the python code corresponding to the Qt GUI you have created. You can run the GUI using python by typing p ython hw.py in command window. It will show the same GUI as seen in the preview from Qt Designer.
Qt is a cross-platform GUI toolkit that scales from mobile interfaces to full blown desktop applications. You can access them using a variety of different programming languages ranging from compiled applications like C++ to scripted languages like Python.
Today's piece is going to be an introduction to using Qt Designer tool to build interfaces for Python applications utilizing PySide bindings Qt. For this tutorial I am assuming my reader knows the basics of both Python and Qt Designer. If you are not familiar with both of these topics check out these resources:
- Beginner's Guide to Python
- Invent with Python 3rd Edition
- A Quick Start to Qt Designer
- Getting to Know Qt Designer
To get started we need to create a simple GUI using Qt Designer:
I have created a MainWindow with a vertical layout that contains QTextEdit and QPushButton widgets. When we save this .ui file it has the following contents:
This xml data is not very useful for our Python application though. To turn it into something we can use we need to convert it to Python code. To do this we use the pyside-uic command. While you are in the same directory as the mainWindow.ui filerun the command:
pyside-uic mainWindow.ui > ui_mainWindow.py
This command will generate a file called ui_mainWindow.py with the following contents:
Note the warning at the top of the file:
WARNING! All changes made in this file will be lost!
Because this file is automatically generated you do not want to make edits directly to it. The reason for this is that they will be overwritten any time you update your GUI in Qt Designer and then regenerate this file.
This means that in order to interact with the GUI that we have created in Qt Designer we need to create a separate file for the rest of our application code. Lets consider the following code:
The first three lines import the sys module and then the components of PySide. The fourth line imports the code our pyside-uic command generated:
Then we create a simple class that utilizes Python's super function to inherit the properties of both PySide's QMainWindow class as well as the properties of the Ui_mainWindow we created in Qt Designer:
After we call the super function we need to tell our application to create the widgets we designed. All files generated by the pyside-uic command require you to call the self.setupUi function to do this.
Next we make our button do something when we press it. We connect the signal clicked to our goPushed function which appends text to the goText entry. As you can see the object names we define in Qt Designer are not arbitrary as they are how we reference the objects in our code.
Then we tell our QMainWindow to make itself visible by calling the show method.
The last thing our application does is start the QApplication main loop that displays our window:
Wrapping Up
Hopefully you found this introduction to building Qt GUIs for your Python applications in Qt Designer useful. If you have any questions about the process detailed here feel free to leave them in a comment below. Also – be sure to check back next week when I go over creating a custom widget in Qt Designer for use with your PySide application.
Resources used in this Lesson: