SoFunction
Updated on 2024-10-30

The whole process of writing a visualization interface in Python (Python+PyCharm+PyQt)

Recently I started to learn Python, but I was limited to reading the theory and compiling a few lines of code, which I found uninteresting, so I wondered if I could use Python to write visual interfaces. So I looked up relevant information and found PyQt. Since I had just seen Qt some time ago and had a good impression of Qt, I thought it should be a more pleasant choice to use PyQt.

1. Preamble

The version of PyQt needs to be consistent with the version of Python, here I use the version of PyQT is PyQt5-5.6-gpl-Py3.5-Qt5.6., specific download, please search directly. Because this version requires v3.5 version of Python, so first you need to install Python3.5, and then install PyQt, in order to write code more conveniently, so the installation of Pycharm, the version is pycharm-community-2017.3. Specific download, installation, there are many tutorials on the Internet, do not describe here, please search for yourself, the following is the first introduction to the Python visualization programming based on Pycharm + Python3.5 + PyQt5.6. At the end of the article, we will also introduce Python visual programming based on the latest version of Pycharm2018.3+Python3.7+PyQt5.11.

2. Start

1), open PyCharm and configure the relevant settings

Open the PyCharm interface as follows:

Click the "Configure" drop-down button at the bottom right of the interface and select "Settings" as shown below:

The following settings screen opens:

As shown in the figure above, in the "Project Interpreter" tab select the version of Python installed on the machine, in general, it will automatically recognize the version of Python installed on the machine, as shown in the figure, the selection of Python3.5;

Once the configuration is complete, click "OK" to return to the start screen.

Select "Create New Project", choose the project path and project name, then click "Create", then the following interface will pop up:

At this point, the work environment is ready;

3. Preparation

1), first, create a .py file, tentatively named

2), Next, you need to import the PyQt file as shown below:

import sys
from  import QWidget, QApplication

Note that if you are prompted that the corresponding file cannot be found, make sure that the installation path of PyQt5 is in the environment variable!

Then add the main function first:

if __name__ == "__main__":
    app = QApplication()
    (app.exec_())

Next you need to add interface related functions:

#class Example
class Example(QWidget):
    def __init__(self):
        super(Example, self).__init__()
        ()
 
    def InitUI(self):
         = QPushButton("Dialog", self)
        (20, 20)
        ()
 
         = QLineEdit(self)
        (130, 22)
 
        ("Input Dialog")
        ()
 
    def ShowDialog(self):
        text,ok = (self, "Input Dialog", "Enter your name:")
        if ok:
            (str(text))

As shown in the above code, the interface is mainly generated in Python through classes, where interface generation, creation of controls, creation of control response function, connect control and control response function can be done.

The function __init__(self) can be interpreted as the constructor of the class, where the initialization of the interface is performed in that part;

The function InitUI(self) mainly generates interface controls;

Once the class has been created, you simply call the class in the main function. As shown below:

if __name__ == "__main__":
    app = QApplication()
    ex = Example()
    (app.exec_())

In the above code, a button (btn), an edit box (le), and a button response function (ShowDialog) are added to the interface, which opens a standard input window in the ShowDialog response function and displays the values entered in the standard input window to the edit box (le).

  

Results:

4. Expansion

In the next made a more complex example, mainly to simulate the solution of the exchange rate, the interface is mainly shown below:

This mainly involves buttons, edit boxes, labels, layouts, etc. .

Written in the same file as the first example, it can be downloaded by clicking on the link:Click to download

5, the latest version of the configuration method

Configure the visual programming environment using the latest versions of Python, PyQt, and Pycharm.

First install Python 3.7, then install Pycharm 2018.3 and configure the python interpreter as a local directory for Python 3.7, the next step is to install PyQt 5.11.

Installation of PyQt5.11 is in the form of pip+wheel file installation, download URL: /project/PyQt5/#files

For pip installation please refer tohttps:///article/

Place the downloaded PyQt5-5.11.3-5.11.2-cp35.cp36.cp37.cp38-none-win_amd64.whl file in the appropriate location.

Open CMD and enter the install command in the following form:

Enter and pip will automatically install the file:

At this point PyQt 5.11 is successfully installed.

This can be tested with the code above.

6, Ubuntu under python2.7 install pyqt5

The above records are the installation of pyqt5 under windows+python3 environment. Due to the work need, need to install pyqt5 in Ubuntu+python2.7 environment, according to the official website of pyqt5, pyqt5 only for python3 installer, but not python2.7, so you can install it by online installation, the following is the installation of pyqt5 in Ubuntu+python2.7 environment:

Open a terminal in ubuntu and type directly:

$sudo apt-get install python-pyqt5

After installing PyQt5, you can import PyQt5 directly in the code editing page, and it is available for testing.

Also, it seems that the above approach can be used to install pyqt5 in ubuntu+python3 environment, also by command:

$sudo apt-get install python3-pyqt5

The above is untested, but should be fine.

summarize

to this article on Python to write a visual interface (Python + PyCharm + PyQt) of the article is introduced to this, more related to Python to write a visual interface content, please search for my previous articles or continue to browse the following related articles I hope you will support me in the future!