pyqt5 QuickStart

  Before using pyqt5, it is recommended to download an Anaconda environment, which makes it more convenient to download the python package. This article is based on the situation where Anaconda has been installed. The IDE is the standard PyCharm.

1. Installation of pyqt package

  Execute the following two installation commands in the python terminal:

pip install PyQt5
pip install PyQt5-tools

  After the installation is successful, first see if you can find QT Designer. The path is: Anaconda\Lib\site-packages\qt5_applications\Qt\bin\designer.exe. If you can see this, the installation is basically successful.
  Reference : https://blog.csdn.net/weixin_43908875/article/details/128419990

2. Using QT Designer (getting ui files)

  Click on designer.exe. At this time you can use graphical methods to design the interface (of course you can also use code design, graphical design is the most basic and simple. It is recommended to use graphical construction for simple frameworks, and use code for subsequent advanced functions) Control), save the file to the project folder after designing (it should be a ui file).
Insert image description here

3. Use External Tools-PYUIC to convert ui files into py files

  After designing the UI, you need to convert it to use it in python. The conversion tool is pyuic. Pyuic is placed in Pycharm's External Tools, but sometimes it does not appear by default, which is the following situation: In this case, we need to add it manually. For the specific adding steps, see this
Insert image description here
  blog : pyqt5: External-tools solution cannot be found in PyCharm
  . After the solution is successfully added, right-click the ui file and select the PyUIC tool of External Tools, and the py file corresponding to the ui will be generated, but only for this interface.

4. Call the interface according to the specified format

  Main program template:

from mainwindow import Ui_MainWindow
import sys
from PyQt5.QtWidgets import QApplication, QMainWindow


def main():
    # 1、创建QApplication类的实例对象
    app = QApplication(sys.argv)
    # 2、创建一个WindowClass实例对象
    myMainWindow = Ui_MainWindow()
    # 3、显示主窗口
    myMainWindow.show()
    # 4、进入程序的主循环、并通过exit函数确保主循环安全结束
    sys.exit(app.exec_())

if __name__ == '__main__':
    main()

  Note: UI_MainWindow is replaced with the py file class generated by yourself.
  After my practice, I found that the py file generated directly with pyuic will report an error. The solution is in this blog: AttributeError: module 'PyQt5.QtGui' has no attribute 'QMainWindow'
  . Follow the above operations and then run the main file to use pyqt. Well, of course this is just a very simple introductory tutorial, more functions need to be learned slowly in the future.

Guess you like

Origin blog.csdn.net/gls_nuaa/article/details/132570824