Objective: To compile a python desktop application.
Note: You can also select vs2017 + qtdesigner, but prefer vscode.
Step 2: Install pyqt5 and PyQt5-tools
Pip3 install-I https://mirrors.aliyun.com/pypi/simple/ pyqt5
Pip3 install-I https://mirrors.aliyun.com/pypi/simple/ PyQt5-tools
Step 2: Find the qtdesigner installation path
Generally, it is automatically installed in the python installation directory. If anaconda3 is installed, it will be found in the following anaconda3 path:
E: \ soft_dp \ anaconda3 \ Lib \ Site-packages \ pyqt5_tools
Generate the desktop shortcut named "designer.exe" in the folder to facilitate future creation of the. UI file.
Step 2: Open designer.exe and draw the UI you want, as shown in:
After the project is created, enter a name and save it to the folder where you want to create the project, for example, welcome. UI.
Step 2: Use vscode to convert the. UI file to a. py file
(1) Open vscode and open the project folder that welcome. UI just stored.
(2) Right-click the welcome. UI and select the operation in the red circle. (This option requires vscode to install the pyqt integration extension in advance)
After you click it, a file named ui_welcome.py is automatically generated, which contains the UI class of the interface just drawn.
Step 2: Create a New. py file to start the program and call the created UI class. The necessary code and explanations are as follows:
1 import sys 2 from pyqt5 import qtcore, qtgui, qtwidgets 3 from pyqt5.qtcore import * 4 from pyqt5.qtwidgets import qfiledialog, qmessagebox, qdockwidget, qlistwidget 5 from pyqt5.qtgui import * 6 7 from ui_welcome import ui_mainwindow # import the created GUI Class 8 9 # create a mywindows class by yourself, and mywindow is your class name. Qtwidgets. qmainwindow: inherits 10 class mywindow (qtwidgets. qmainwindow, ui_mainwindow): 11 #__ init __: destructor, that is, the project that will be preloaded after the class is created. 12 # Run immediately. This method can be used to initialize your object as expected. 13 def _ init _ (Self): 14 # Here we need to reload mywindow, and it also contains the pre-add-on of qtwidgets. qmainwindow. 15 super (mywindow, self ). _ init _ () 16 self. setupui (Self) 17 18 19 if _ name _ = '_ main _': # If the whole program is the main program 20 # qapplication is equivalent to the main function, that is, the main entry function of the entire program (many files. 21 # There must be at least one such instance for GUI programs to run. 22 APP = qtwidgets. qapplication (SYS. argv) 23 # generate an instance of the mywindow class. 24 window = mywindow () 25 # With an instance, You have to display it. Show () is the qwidget method used to display the window. 26 window. Show () 27 # Call the exitout function of sysdatabase. The condition is app.exe C _ (), that is, the whole window is closed. 28 # When the program is returned, sys.exit(app.exec_())), change to app.exe C _ () It's okay 29 # https://stackoverflow.com/questions/25719524/difference-between-sys-exitapp-exec-and-app-exec30 sys.exit(app.exe C _())
Then, press F5 to run the file, and the created window will pop up:
Build and test the vscode + pyqt5 + qtdesigner Environment