03 A simple example of the first application based on PyQt5
1 Import necessary classes
- Two classes are required
Application,QWidget
;
- These two classes inherit from
QtWidgets
;
Application
It is the application class and QWidget
the window class;
sys
Modules are used by application or window parameters.
import sys
from PyQt5.QtWidgets import QApplication, QWidget
2 Create an instance of the Application class
app = QApplication(sys.argv)
3 Create windows and related operations
widget = QWidget()
widget.resize(500, 450)
- Move the window (that is, change the zero coordinate of the upper left corner):
widget.move(350, 500)
widget.setWindowTitle("第一个PyQt5应用")
widget.show()
4 Safe exit
sys.exit(app.exec_())
5 complete code
import sys
from PyQt5.QtWidgets import QApplication, QWidget
if __name__ == "__main__":
app = QApplication(sys.argv)
widget = QWidget()
widget.resize(500, 450)
widget.move(350, 500)
widget.setWindowTitle("第一个PyQt5应用")
widget.show()
sys.exit(app.exec_())
6 Operation effect
7 Parameters of Qt Designer, PyUIC5, PyRcc5
parameter |
Qt Designer |
PyUIC5 |
PyRcc5 |
illustrate |
Assisted rapid design program |
Convert .ui files into Python code |
Compile resource files (such as icons, style sheets, etc.) into Python modules |
Name |
Qt Designer |
PyUIC5 |
PyRcc5 |
program |
D:\Python 3.5\Lib\site-packages\pyqt5_tools\Qt\bin\designer.exe (Just change it to your own directory) |
D:\Python 3.5\Scripts\pyuic5.exe |
D:\Python 3.5\Scripts\pyrcc5.exe |
arguments |
$FileDir$\$FileName$ |
$FileName$ -o $FileNameWithoutExtension$.py |
$FileName$ -o $FileNameWithoutExtension$.py |
working directory |
$FileDir$ |
$FileDir$ |
$FileDir$ |