PyQt5 (External Tools) is successfully configured in Pycharm, and the interface is designed to directly generate python code

1. Install PyQt5 and PyQt5-tools

Set up the Python environment in Pycharm, click File-Settings-Project-Python Interpreter

 Exit after setting, click Terminal under the window, enter

# 直接安装输入pip install pyqt5,如果太慢可以用国内镜像源,若出错多试几次
pip install pyqt5 -i https://pypi.tuna.tsinghua.edu.cn/simple

 Install PyQt5-tools in the same way:

pip install pyqt5-tools -i https://pypi.tuna.tsinghua.edu.cn/simple

 2. Configuration environment

Click File-Settings-Tools-External Tools, click the + sign

 

 Three tools need to be configured, namely QtDesigner, PyUICS, and Pyrcc, among which QtDesigner is a qt designer, PyUics is to convert the UI interface into a py file, and Pyrcc is a resource system conversion.

Configure Qt Designer, as shown in the figure:

 Name: Qt Designer (custom)
Program: C:\Users\wmm\AppData\Local\Programs\Python\Python39\Lib\site-packages\qt5_applications\Qt\bin\designer.exe (change to your own path, you can Search for desinger.exe in your own Python installation directory, and then fill in the path)
Working directory: working directory, just fill in $FileDir$.

 

Configure PyUIC, as shown in the figure:

Program: C:\Users\wmm\AppData\Local\Programs\Python\Python39\python.exe (change to your own path, you can search for python.exe in your own Python installation directory, and then fill in the path)
Arguments: -m PyQt5.uic.pyuic $FileName$ -o $FileNameWithoutExtension$.py
Working directory: working directory, fill in $FileDir$

 Configure Pyqcc, as shown in the figure:

Program: C:\Users\wmm\AppData\Local\Programs\Python\Python39\Scripts\pyrcc5.exe
Arguments: $FileName$ -o $FileNameWithoutExtension$_rc.py (change to your own path, you can install it in your own Python Search for pyrcc5.exe in the directory, and then fill in the path)
Working directory: the working directory, just fill in $FileDir$.

 

 So far, the variables that need to be configured have been completed.

3. Test

1) New construction

File-> New Project, as shown below:

Select Previously configured interpreter

If you choose New environment using-Virtualenv, note: Be sure to check the option in the red box, otherwise you will report ModuleNotFoundError: No module named 'PyQt5'various errors such as not found

2) Open the ui design window

Create a new py file in the project (main.py will be created automatically when creating a new project), enter the file, right-click with the mouse, select External Tools-QtDesigner, then create a window with Widget, then drag a Qlabel, enter hello world

After designing the interface, click Save to save it in the project folder and exit.

 

 3) Generate executable py file

You can see the saved uitest.ui file in the project, right click to find External Tools, select PyUIC, and generate the corresponding py file

 

 4) Modify main.py, run the program, and you can see the interface effect we designed

import sys
import uitest  # 对应uitest.py
from PyQt5.QtWidgets import QApplication, QMainWindow

if __name__ == '__main__':
    app = QApplication(sys.argv)
MainWindow = QMainWindow()
ui = hello.Ui_Form()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())

 At this point, you're done! !

4. About the role of the main function app=QApplication(sys.argv) and sys.exit(app.exec_()) in PyQt

1) The function of app.exec_() is to run the main loop. This function must be called to start event processing. Call this method to enter the main loop of the program until the end of calling exit(). The main event loop receives events from the window system and dispatches them to application widgets. If there is no such method, the main loop of the program will end directly when running, so the window will flash back when running.
app.exec_() will return a status code when exiting
2) Instead of sys.exit(app.exec_()), only use app.exec_(), the program can run normally, but the process will not exit after closing the window.
The function of sys.exit(n) is to exit the application and return n to the parent process.
3) QApplication describes in detail
the QApplication class manages the control flow and main settings of GUI applications. It can be said that QApplication is the lifeblood of Qt's entire background management

It contains the main event loop where all events from the window system and other resources are processed and dispatched. It also handles application initialization and termination, and provides dialog management. It also handles most system-wide and application-wide settings.

For any GUI application using Qt, there is exactly one QApplication object, regardless of whether the application has 0, 1, 2 or more windows at the same time.

The QApplication object is accessible through the global variable qApp. Its main areas of responsibility are:

It uses the user's desktop settings, such as palette(), font(), and doubleClickInterval(), to initialize the application. If the user changes the global desktop, for example via some control panel, it keeps track of these properties.

It performs event handling, that is, it receives events from the underlying window system and dispatches them to the relevant widgets. You can send your own events to widgets by using sendEvent() and postEvent().

It parses command line arguments and sets internal state based on them. See the constructor documentation below for details on this.

It defines the look and feel of an application encapsulated by a QStyle object. In the running state, it can be changed by setStyle().

It specifies how the application assigns colors. Please refer to setColorSpec() for details.

It defines the default text encoding (see setDefaultCodec()) and provides user-visible localized strings via translate().

It provides some magic objects like desktop() and clipboard().

It knows about the application's window. You can use widgetAt() to ask which widgets exist at a certain point, get a list of topLevelWidgets() (top-level widgets) and close all windows with closeAllWindows(), etc.

It manages the application's mouse cursor handling, see setOverrideCursor() and setGlobalMouseTracking().

On the X Window System, it provides functions to flush and synchronize the communication stream, please refer to flushX() and syncX().

It provides sophisticated dialog management support. This makes it possible for the application to gracefully end when the user logs out, undoing the shutdown process if it cannot be terminated and even preserving the state of the entire application for future conversations. For details, please refer to isSessionRestored(), sessionId(), commitData(), and saveState().

The application rehearsal example contains a typical complete main() for common usage of QApplication.

Because the QApplication object does so much initialization, it must be created before all other classes related to the user interface are created.

Since it also handles command-line arguments, it's usually a good idea to create it before argv is interpreted and modified in the application. (Note, also for X11, that setMainWidget() can change the main widget according to the -geometry option. To keep this functionality, you must set your default before setMainWidget() and any of its overloads.)

QApplication::QApplication ( int & argc, char ** argv )

Initializes the window system and constructs an application object using the argc command-line arguments in argv.

The global pointer qApp points to this application object. There should be only one application object created.

This application object must be constructed before any drawing devices (including widgets, pixmaps, bitmaps, etc.).

argc and argv are the parameters passed in from the command line. For example, enter a command
cp file.c file1.c in linux
, then argc=3 argv is the string array of the above line.
Because graphics programming sometimes needs to pass parameters to the program from the command line, there are argc and argv.
(Original link: https://blog.csdn.net/cuicui_ruirui/article/details/102873528)

Guess you like

Origin blog.csdn.net/weixin_42149550/article/details/131455208