PyQt is a set of Python bindings developed for Nokia's Qt application framework. Moreover, the PyQt framework runs on all platforms supported by Qt including Mac OS X, Windows and Linux. There are two different sets of bindings: PyQt5 supports Qt v5; and the older PyQt4 supports Qt v4 and earlier. The PyQt module can be used to create desktop applications with Python. In this article you'll learn how to install the PyQt module. Desktop applications made with PyQt are cross platform, they will work on Microsoft Windows, Apple Mac OS X and Linux computers (including Raspberry Pi).
This tutorial requires Qt Creator to be installed — you can download it free from the Qt website. Go to https://www.qt.io/download and download the Qt package. You can opt to install only Creator during the installation.
Open up Qt Creator and you will be presented with the main window. The designer is available via the tab on the left hand side. However, to activate this you first need to start creating a .ui
file.
To create a .ui
file go to File -> New File or Project.. In the window that appears select Qt under Files and Classes on the left, then select Qt Designer Form on the right. You'll notice the icon has 'ui' on it, showing the type of file you're creating.
In the next step you'll be asked what type of widget you want to create. If you are starting an application then Main Window is the right choice. However, you can also create .ui
files for dialog boxes, forms and custom compound widgets.
Next choose a filename and save folder for your file. Save your .ui
file with the same name as the class you'll be creating, just to make make subsequent commands simpler.
Finally, you can choose to add the file to your version control system if you're using one. Feel free to skip this step — it doesn't affect your UI.
Laying out your Main Window
You'll be presented with your newly created main window in the UI designer. There isn't much to see to begin with, just a grey working area representing the window, together with the beginnings of a window menu bar.
You can resize the window by clicking the window and dragging the blue handles on each corner.
The first step in building an application is to add some widgets to your window. In our first applications we learnt that to set the central widget for a QMainWindow
we need to use .setCentralWidget()
. We also saw that to add multiple widgets with a layout, we need an intermediary QWidget
to apply the layout to, rather than adding the layout to the window directly.
Try an alternative like AbiWord. AbiWord can read and write all standard industry document formats, ranging from open source efforts such as those created in OpenOffice, to those created in commercial apps such as Microsoft Word. Abiword for mac os x. AbiWord is a free word processing program similar to Microsoft Word. It is suitable for typing papers, letters, reports, memos, and so forth.
Qt Creator takes care of this for you automatically, although it's not particularly obvious about it.
To add multiple widgets to the main window with a layout, first drag your widgets onto the QMainWindow
. Here we're dragging 3 labels. It doesn't matter where you drop them.
We've created 2 widgets by dragging them onto the window, made them children of that window. We can now apply a layout.
Find the QMainWindow
in the right hand panel (it should be right at the top). Underneath you see centralwidget representing the window's central widget. The icon for the central widget show the current layout applied. Initially it has a red circle-cross through it, showing that there is no layout active.
Right click on the QMainWindow
object, and find 'Layout' in the resulting dropdown.
Next you'll see a list of layouts which you can apply to the window. Safari update for mac. Select Lay Out Horizontally and the layout will be applied to the widget.
The selected layout is applied to the the centralwidget of the QMainWindow
and the widgets are added the layout, being laid out depending on the selected layout. Note that in Qt Creator you can actually drag and re-order the widgets within the layout, or select a different layout, as you like. This makes it especially nice to prototyping and trying out things.
Using your generated .ui file
We've created a very simple UI. The next step is to get this into Python and use it to construct a working application.
First save your .ui
file — by default it will save at the location you chosen while creating it, although you can choose another location if you like.
The .ui
file is in XML format. To use our UI from Python we have two alternative methods available —
- load into into a class using the
.loadUI()
method - convert it to Python using the
pyuic5
tool.
These two approaches are covered below. Personally I prefer to convert the UI to a Python file to keep things similar from a programming & packaging point of view.
Loading the .ui file directly
To load .ui
files we can use the uic
module included with PyQt5, specifically the uic.loadUI()
method. This takes the filename of a UI file and loads it creating a fully-functional PyQt5 object.
For PySide2 you can use a QUiLoader
instance and call the loader.load()
method to accomplish the same thing.
- PyQt5
- PySide2
As the uid.loadUI()
method turns an instance object you cannot attach custom __init__()
code. You can however handle this through a custom setup function
To load a UI from the __init__
block of an existing widget (e.g. a QMainWindow
) you can use uic.loadUI(filename, self)
for PyQt5.
The PySide2 loader does not support this approach — the second parameter is for the parent widget of the widget you're creating. This prevents you adding custom code to the initialization of the widget, but you can work around this with a separate init function.
Converting your .ui file to Python
To generate a Python output file run pyuic5
from the command line, passing the .ui
file and the target file for output, with a -o
parameter. The following will generate a Python file named MainWindow.py
which contains our created UI.
If you're using PyQt4 the tool is named `pyuic4`, but is otherwise completely identical.
You can open the resulting MainWindow.py
file in an editor to take a look, although you should not edit this file. The power of using Qt Creator is being able to edit, tweak and update your application while you develop. Any changes made to this file will be lost when you update it. However, you can override and tweak anything you like when you import and use the file in your applications.
Importing the resulting Python file works as for any other. You can import your class as follows. The pyuic5
tool appends Ui_
to the name of the object defined in Qt Creator, and it is this object you want to import.
To create the main window in your application, create a class as normal but subclassing from both QMainWindow
and your imported Ui_MainWindow
class. Finally, call self.setupUi(self)
from within the __init__
to trigger the setup of the interface.
This produces exactly the same result as before.
That's it. Your window is now fully set up. Since the use of a .ui file abstracts out the UI-specific code, you can use this same pattern to load any interface you design.
Adding application logic
You can interact with widgets created through Qt Creator just as you would those created with code. To make things simpler uic
adds all child widgets to the window object by their id name as specified in Qt Creator. We'll cover how to work with these in the next part.
Requirements¶
Qt package from here or a custom build of Qt (preferablyQt 5.12 or greater)
A Python interpreter (version Python 3.5+ or Python 2.7).You can use the one provided by HomeBrew, or you can getpython from the official website.
XCode 8.2 (macOS 10.11), 8.3.3 (macOS 10.12), 9 (macOS 10.13), 10.1 (macOS 10.14)
CMake version 3.1 or greater
Git version 2 or greater
libclang from your system or the prebuilt version from the
QtDownloads
page is recommended.virtualenv
is strongly recommended, but optional.sphinx
package for the documentation (optional).Depending on your OS, the following dependencies might alsobe required:
libgl-dev
,python-dev
,python-distutils
,and
python-setuptools
.
Building from source¶
Creating a virtual environment¶
virtualenv
allows you to create a local, user-writeable copy of a python environment intowhich arbitrary modules can be installed and which can be removed after use:
will create and use a new virtual environment, which is indicated by the command prompt changing.
Setting up CLANG¶
If you don't have libclang already in your system, you can download from the Qt servers:
Python Qt For Mac Shortcut
Extract the files, and leave it on any desired path, and then set these two requiredenvironment variables:
Getting PySide2¶
Cloning the official repository can be done by:
Checking out the version that we want to build, e.g. Enterprise architect for mac os x. 5.14:
Note
Keep in mind you need to use the same version as your Qt installation
Building PySide2¶
Python Qt For Mac Osx
Check your Qt installation path, to specifically use that version of qmake to build PySide2.e.g. /opt/Qt/5.14.0/gcc_64/bin/qmake
.
Build can take a few minutes, so it is recommended to use more than one CPU core:
Installing PySide2¶
To install on the current directory, just run:
Python Qt For Mac Os
Test installation¶
The first step in building an application is to add some widgets to your window. In our first applications we learnt that to set the central widget for a QMainWindow
we need to use .setCentralWidget()
. We also saw that to add multiple widgets with a layout, we need an intermediary QWidget
to apply the layout to, rather than adding the layout to the window directly.
Try an alternative like AbiWord. AbiWord can read and write all standard industry document formats, ranging from open source efforts such as those created in OpenOffice, to those created in commercial apps such as Microsoft Word. Abiword for mac os x. AbiWord is a free word processing program similar to Microsoft Word. It is suitable for typing papers, letters, reports, memos, and so forth.
Qt Creator takes care of this for you automatically, although it's not particularly obvious about it.
To add multiple widgets to the main window with a layout, first drag your widgets onto the QMainWindow
. Here we're dragging 3 labels. It doesn't matter where you drop them.
We've created 2 widgets by dragging them onto the window, made them children of that window. We can now apply a layout.
Find the QMainWindow
in the right hand panel (it should be right at the top). Underneath you see centralwidget representing the window's central widget. The icon for the central widget show the current layout applied. Initially it has a red circle-cross through it, showing that there is no layout active.
Right click on the QMainWindow
object, and find 'Layout' in the resulting dropdown.
Next you'll see a list of layouts which you can apply to the window. Safari update for mac. Select Lay Out Horizontally and the layout will be applied to the widget.
The selected layout is applied to the the centralwidget of the QMainWindow
and the widgets are added the layout, being laid out depending on the selected layout. Note that in Qt Creator you can actually drag and re-order the widgets within the layout, or select a different layout, as you like. This makes it especially nice to prototyping and trying out things.
Using your generated .ui file
We've created a very simple UI. The next step is to get this into Python and use it to construct a working application.
First save your .ui
file — by default it will save at the location you chosen while creating it, although you can choose another location if you like.
The .ui
file is in XML format. To use our UI from Python we have two alternative methods available —
- load into into a class using the
.loadUI()
method - convert it to Python using the
pyuic5
tool.
These two approaches are covered below. Personally I prefer to convert the UI to a Python file to keep things similar from a programming & packaging point of view.
Loading the .ui file directly
To load .ui
files we can use the uic
module included with PyQt5, specifically the uic.loadUI()
method. This takes the filename of a UI file and loads it creating a fully-functional PyQt5 object.
For PySide2 you can use a QUiLoader
instance and call the loader.load()
method to accomplish the same thing.
- PyQt5
- PySide2
As the uid.loadUI()
method turns an instance object you cannot attach custom __init__()
code. You can however handle this through a custom setup function
To load a UI from the __init__
block of an existing widget (e.g. a QMainWindow
) you can use uic.loadUI(filename, self)
for PyQt5.
The PySide2 loader does not support this approach — the second parameter is for the parent widget of the widget you're creating. This prevents you adding custom code to the initialization of the widget, but you can work around this with a separate init function.
Converting your .ui file to Python
To generate a Python output file run pyuic5
from the command line, passing the .ui
file and the target file for output, with a -o
parameter. The following will generate a Python file named MainWindow.py
which contains our created UI.
If you're using PyQt4 the tool is named `pyuic4`, but is otherwise completely identical.
You can open the resulting MainWindow.py
file in an editor to take a look, although you should not edit this file. The power of using Qt Creator is being able to edit, tweak and update your application while you develop. Any changes made to this file will be lost when you update it. However, you can override and tweak anything you like when you import and use the file in your applications.
Importing the resulting Python file works as for any other. You can import your class as follows. The pyuic5
tool appends Ui_
to the name of the object defined in Qt Creator, and it is this object you want to import.
To create the main window in your application, create a class as normal but subclassing from both QMainWindow
and your imported Ui_MainWindow
class. Finally, call self.setupUi(self)
from within the __init__
to trigger the setup of the interface.
This produces exactly the same result as before.
That's it. Your window is now fully set up. Since the use of a .ui file abstracts out the UI-specific code, you can use this same pattern to load any interface you design.
Adding application logic
You can interact with widgets created through Qt Creator just as you would those created with code. To make things simpler uic
adds all child widgets to the window object by their id name as specified in Qt Creator. We'll cover how to work with these in the next part.
Requirements¶
Qt package from here or a custom build of Qt (preferablyQt 5.12 or greater)
A Python interpreter (version Python 3.5+ or Python 2.7).You can use the one provided by HomeBrew, or you can getpython from the official website.
XCode 8.2 (macOS 10.11), 8.3.3 (macOS 10.12), 9 (macOS 10.13), 10.1 (macOS 10.14)
CMake version 3.1 or greater
Git version 2 or greater
libclang from your system or the prebuilt version from the
QtDownloads
page is recommended.virtualenv
is strongly recommended, but optional.sphinx
package for the documentation (optional).Depending on your OS, the following dependencies might alsobe required:
libgl-dev
,python-dev
,python-distutils
,and
python-setuptools
.
Building from source¶
Creating a virtual environment¶
virtualenv
allows you to create a local, user-writeable copy of a python environment intowhich arbitrary modules can be installed and which can be removed after use:
will create and use a new virtual environment, which is indicated by the command prompt changing.
Setting up CLANG¶
If you don't have libclang already in your system, you can download from the Qt servers:
Python Qt For Mac Shortcut
Extract the files, and leave it on any desired path, and then set these two requiredenvironment variables:
Getting PySide2¶
Cloning the official repository can be done by:
Checking out the version that we want to build, e.g. Enterprise architect for mac os x. 5.14:
Note
Keep in mind you need to use the same version as your Qt installation
Building PySide2¶
Python Qt For Mac Osx
Check your Qt installation path, to specifically use that version of qmake to build PySide2.e.g. /opt/Qt/5.14.0/gcc_64/bin/qmake
.
Build can take a few minutes, so it is recommended to use more than one CPU core:
Installing PySide2¶
To install on the current directory, just run:
Python Qt For Mac Os
Test installation¶
You can execute one of the examples to verify the process is properly working.Remember to properly set the environment variables for Qt and PySide2:
Python Qt For Mac Installer
© 2020 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.