python qt5 安装

1、安装环境

windows+pycharm

2、安装PyQt5

pip install PyQt5

3、安装PyQt5-tools

pip install PyQt5-tools

4、配置pycharm

配置PyCharm是为了在Pycharm里面实现打开qt designer,然后生成qt文件,然后将qt文件转换成python语言的软件文件。 

4.1、配置designer.exe

选择File->Settings->Tools->External Tools->点击+

Name:自定义 ,自己认识就好
Program:指向上述安装PyQt5-tools里面的designer.exe,designer.exe这个文件找不到的话就在pyqt5_tools下全局搜索一下,因为这个的存放位置在以前是在pyqt5_tools目录下,现在放在Qt/bin/下,所以全局搜索一下最稳妥
Work directory:使用变量 $FileDir$

4.2、配置将.ui转换成.py的工具

Name:自定义 ,自己认识就好

Program:python的安装路径的python.exe路径

Arguments: -m PyQt5.uic.pyuic  $FileName$ -o $FileNameWithoutExtension$.py

Work directory:使用变量 $FileDir$

5、简单使用

5.1、新建一个空的项目

5.2、运行Qt Designer

点击Tools->External Tools->Qt Designer

保存退出

5.3、 将.ui文件转换成.py文件

5.4、使用代码

from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtWidgets import QApplication,QMainWindow
import sys

class Ui_Form(object):

    def setupUi(self, Form):
        Form.setObjectName("Form")
        Form.resize(400, 300)
        self.pushButton = QtWidgets.QPushButton(Form)
        self.pushButton.setGeometry(QtCore.QRect(130, 200, 75, 23))
        self.pushButton.setObjectName("pushButton")
        self.label = QtWidgets.QLabel(Form)
        self.label.setGeometry(QtCore.QRect(50, 90, 291, 61))
        self.label.setObjectName("label")
        self.retranslateUi(Form)
        QtCore.QMetaObject.connectSlotsByName(Form)
        self.pushButton.clicked.connect(self.slot_btn_clicked)

    def retranslateUi(self, Form):
        _translate = QtCore.QCoreApplication.translate
        Form.setWindowTitle(_translate("Form", "Form"))
        self.pushButton.setText(_translate("Form", "PushButton"))
        self.label.setText(_translate("Form", "TextLabel"))

    def slot_btn_clicked(self):
        self.label.setText("hello world!!!")

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

5.5、结果

 

pyqt5的学习可以看这个:https://github.com/PyQt5/PyQt

6、问题

6.1、pyqt5-tools 使用qtdesigner错误-This application failed to start because no Qt platform plugin could be i…

原因:pyqt5的plugins路径找不到

解决方案:

  1. 将pyqt5的plugins路径加入环境变量
QT_QPA_PLATFORM_PLUGIN_PATH

C:\Users\45906\AppData\Local\Programs\Python\Python37\Lib\site-packages\pyqt5_tools\Qt\bin\plugins
  1. 将pyqt5的plugins文件夹复制到designer.exe所在目录下