Python3安装配置PyQt5以及PyCharm下PyQt5的测试使用

PyQt5的配置和使用

    • 零、背景
    • 一、环境
    • 二、下载安装软件
    • 三、配置Qt5 Desinger和PyUIC
    • 四、测试代码

零、背景

Python是一门简单易学的语言,上手快、框架多,PyQt5是一个优秀的界面开发框架,起源于C++中的Qt框架,开发可视化界面非常方便,下面说一下自己在Python3中使用PyQt5的一些心得和方法。

一、环境

win7 64位
python3.6.4

二、下载安装软件

1、安装Qt5.12.4版本。
2、PyCharm下安装PyQt5、PyQt5-sip、PyQt5-stubs等,注意选择和Qt5.12对应的版本,安装方法如下:
Python3安装配置PyQt5以及PyCharm下PyQt5的测试使用_第1张图片

3、此时可以写一个demo测试PyQt5是否安装成功,测试代码如下:

import sys
from PyQt5 import QtWidgets,QtCore

app = QtWidgets.QApplication(sys.argv)
widget = QtWidgets.QWidget()
widget.resize(400,100)
widget.setWindowTitle("this is a demo for PyQy Widget!")
widget.show()

sys.exit(app.exec_())

运行效果如下:
Python3安装配置PyQt5以及PyCharm下PyQt5的测试使用_第2张图片

三、配置Qt5 Desinger和PyUIC

1、配置Qt5 Desinger,见截图:
Python3安装配置PyQt5以及PyCharm下PyQt5的测试使用_第3张图片
2、配置PyUIC,截图如下:
Python3安装配置PyQt5以及PyCharm下PyQt5的测试使用_第4张图片

PyUIC转换指令参数:-m PyQt5.uic.pyuic  $FileName$ -o $FileNameWithoutExtension$.py

***特别注意:***这里一定要注意你选择的python.exe是你设置的虚拟环境下的所在路径,如果你使用的是本机真实环境的话,就配置本地安装的python.exe所在路径,否则使用PyUIC转换python代码时,会报下面的错误:
“D:\Program Files\Python36\python.exe: Error while finding module specification for ‘PyQt5.uic.pyuic’ (ModuleNotFoundError: No module named ‘PyQt5’)”
在这里插入图片描述

3、Qt5 Desinger使用:
在Pycharm菜单上选择:Toos-》External Tools->Qt5 Designer

会弹出设计师的界面,这里简单绘制了一个界面,截图如下:
Python3安装配置PyQt5以及PyCharm下PyQt5的测试使用_第5张图片
点击左上角的保存按钮->命名,会在工程中看到一个,我这里命名为hello.ui,截图如下:
在这里插入图片描述
上图中的hello.py是我写博客之前已经完成转化的。

4、PyUIC的使用,选中hello.ui右键-》External Tools->PyUIC,即可:
Python3安装配置PyQt5以及PyCharm下PyQt5的测试使用_第6张图片

四、测试代码

经过上面第4步转化后的代码,如下:

# -*- coding: utf-8 -*-

# Form implementation generated from reading ui file 'hello.ui'
#
# Created by: PyQt5 UI code generator 5.12.1
#
# WARNING! All changes made in this file will be lost!


from PyQt5 import QtCore, QtGui, QtWidgets


class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
        MainWindow.setObjectName("MainWindow")
        MainWindow.resize(800, 600)
        self.centralwidget = QtWidgets.QWidget(MainWindow)
        self.centralwidget.setObjectName("centralwidget")
        self.label = QtWidgets.QLabel(self.centralwidget)
        self.label.setGeometry(QtCore.QRect(250, 145, 261, 101))
        self.label.setObjectName("label")
        MainWindow.setCentralWidget(self.centralwidget)
        self.menubar = QtWidgets.QMenuBar(MainWindow)
        self.menubar.setGeometry(QtCore.QRect(0, 0, 800, 23))
        self.menubar.setObjectName("menubar")
        MainWindow.setMenuBar(self.menubar)
        self.statusbar = QtWidgets.QStatusBar(MainWindow)
        self.statusbar.setObjectName("statusbar")
        MainWindow.setStatusBar(self.statusbar)

        self.retranslateUi(MainWindow)
        QtCore.QMetaObject.connectSlotsByName(MainWindow)

    def retranslateUi(self, MainWindow):
        _translate = QtCore.QCoreApplication.translate
        MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
        self.label.setText(_translate("MainWindow", "hello python"))

#增加main函数,调用上面的类,下面的代码是自己写的,不会自动转化出来的
import sys
from PyQt5.QtWidgets import QApplication, QMainWindow
if __name__ == '__main__':
    app = QApplication(sys.argv)
    MainWindow = QMainWindow()
    ui = Ui_MainWindow()
    ui.setupUi(MainWindow)
    MainWindow.show()
    sys.exit(app.exec_())

运行效果如下:
Python3安装配置PyQt5以及PyCharm下PyQt5的测试使用_第7张图片

完!

你可能感兴趣的:(python编程,No,module,name)