Setting Up PyQt in PyCharm for Graphical Interface Development
PyQt leverages the combination of Python programming language with the power and versatility of the Qt framwork, enabling developers to create feature-rich graphical user interfaces (GUIs). This guide outlines the steps for setting up PyQt within PyCharm, including creating and utilizing graphical components designed with Qt Designer.
Prerequisites
Ensure the following software versions are installed:
- PyCharm Cmomunity Edition (Version 2020.2.1 x64)
- Python (Version 3.7.0 x32)
Installation of PyQt and Related Packages
Install the PyQt module along with its associated tools and stubs using pip:
pip install pyqt5
pip install pyqt5-tools
pip install pyqt5-stubs
For slow downolad speeds, utilize a mirror such as Tsinghua University:
pip install pyqt5 -i https://pypi.tuna.tsinghua.edu.cn/simple
pip install pyqt5-tools -i https://pypi.tuna.tsinghua.edu.cn/simple
Configure External Tools in PyCharm
Qt Designer
Add Qt Designer to the External Tools section in PyCharm:
- Navigate to
File > Settings > Tools > External Tools. - Add a new tool with the following configuraton:
| Field | Value |
|---------------------|----------------------------------------------------------|
| Name | QtDesigner |
| Program | `C:\Python\Python37-32\Lib\site-packages\qt5_applications\Qt\bin\designer.exe` |
| Working Directory | `$ProjectFileDir$/ui` |
PyUIC (Modular Mode)
Configure PyUIC as a modular tool to convert .ui files to Python modules:
| Field | Value |
|---------------------|-----------------------------------------------------------|
| Name | PyUIC Modular |
| Program | `C:\Python\Python37-32\python.exe` |
| Arguments | `-m PyQt5.uic.pyuic $FileName$ -o $FileNameWithoutExtension$_designed.py` |
| Working Directory | `$FileDir$` |
PyUIC (Direct Mode)
Alternatively, use PyUIC directly:
| Field | Value |
|---------------------|-----------------------------------------------------------|
| Name | PyUIC Direct |
| Program | `C:\Python\Python37-32\Scripts\pyuic5.exe` |
| Arguments | `$FileName$ -o $FileNameWithoutExtension$_designed.py` |
| Working Directory | `$FileDir$` |
Using Qt Designer to Create a GUI
- Open Qt Designer from PyCharm.
- Design a simple window interface with a button and define its properties, such as renaming it and setting the window title.
- Save the layout as
Test.uiin the designateduidirectory. - Use the configured PyUIC tool to generate a Python module, e.g.,
test_designed.py.
Incorporating Generated Python Code into Projects
Direct Utilization of the Generated Code
The generated Python code can be used directly:
import sys
from PyQt5 import QtWidgets
from test_designed import Ui_Dialog
if __name__ == "__main__":
application = QtWidgets.QApplication(sys.argv)
window = QtWidgets.QWidget()
ui = Ui_Dialog()
ui.setupUi(window)
window.show()
sys.exit(application.exec_())
Extending Functionality Through Inheritance
The generated class can also be inherited for extension:
import sys
from PyQt5 import QtWidgets
from test_designed import Ui_Dialog
class ApplicationWindow(QtWidgets.QWidget, Ui_Dialog):
def __init__(self):
super().__init__()
self.setupUi(self)
if __name__ == "__main__":
application = QtWidgets.QApplication(sys.argv)
window = ApplicationWindow()
window.show()
sys.exit(application.exec_())