Fading Coder

An Old Coder’s Final Dance

You are here: Home > Tech > Content

Setting Up PyQt in PyCharm for Graphical Interface Development

Tech 5

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:

  1. Navigate to File > Settings > Tools > External Tools.
  2. 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

  1. Open Qt Designer from PyCharm.
  2. Design a simple window interface with a button and define its properties, such as renaming it and setting the window title.
  3. Save the layout as Test.ui in the designated ui directory.
  4. 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_())

Related Articles

Understanding Strong and Weak References in Java

Strong References Strong reference are the most prevalent type of object referencing in Java. When an object has a strong reference pointing to it, the garbage collector will not reclaim its memory. F...

Comprehensive Guide to SSTI Explained with Payload Bypass Techniques

Introduction Server-Side Template Injection (SSTI) is a vulnerability in web applications where user input is improper handled within the template engine and executed on the server. This exploit can r...

Implement Image Upload Functionality for Django Integrated TinyMCE Editor

Django’s Admin panel is highly user-friendly, and pairing it with TinyMCE, an effective rich text editor, simplifies content management significantly. Combining the two is particular useful for bloggi...

Leave a Comment

Anonymous

◎Feel free to join the discussion and share your thoughts.