PyQt【第五篇】 PyQt5与Pycharm的极速 GUI 开发

安装

为避免不兼容,均使用pip方式安装

Python安装

开发环境:

python3.5.2 64

win10 64bit

PyQt安装

pip install PyQt5

3.测试是否安装成功

import PyQt5

没报错说明安装成功

Eric6安装

可pip或源码安装

Eric6官网:http://eric-ide.python-projects.org/eric-download.html

1.安装依赖:pip install QScintilla

2.下载:eric6-17.03.1.zip并解压,找到install.py文件打开方式改为刚刚安装的python程序

3.安装:然后双击该文件

4,启动:然后找到...python\Lib\site-packages\eric6目录下的eric6.py文件,双击启动eric6

 

配置Eric6

 如果没有自动弹出配置窗口的话自己手动打开配置窗口:设置->首选项。

示例

建立项目

打开Eric6,菜单栏中选择项目-新建 

添加项目名称并创立项目目录

 

 

 在打开的选择项目文件夹对话框中右键点击空白处,在弹出的菜单中选择新建-文件夹 

 

回到之前的界面,点"OK"

 

观察程序主界面的标题栏发现显示Hello – eric6。在项目浏览器的顶部源代码标签里自动为我们生成一个_init_.py文件,该文件中没有任何代码,我们不用去理会。 

创建窗体

点击源代码标签右边的窗体标签,在标签下面的空白处右键点击,弹出的菜单选择新建窗体 

 弹出新建窗体对话框,可以选择其它窗体类型,这里我们使用默认对话框类型,点击OK按钮

给窗体命名,双击窗体也是这个错误

 

 

pip install PyQt5-tools -i https://pypi.douban.com/simple   # Python\Python35-32\Lib\site-packages\目录下新增一个pyqt5-tools文件夹

把目录pyqt5-tools文件夹下的两个文件拷贝到目python35\Lib\site-packages\PyQt5\Qt\bin

设计窗体

双击窗体,计算机会直接打开Qt设计师,并自动将HelloWindow.ui加载到程序中 

 

配置Pycharm

配置环境

settings->tools->external tools

D:\app\python3\Lib\site-packages\pyqt5-tools\designer.exe
$ProjectFileDir$

D:\app\python3\python.exe
-m PyQt5.uic.pyuic  $FileName$ -o $FileNameWithoutExtension$.py
$FileDir$

D:\app\python3\Scripts\pyrcc5.exe 
$FileName$ -o $FileNameWithoutExtension$_rc.py
D:\app\python3\Scripts

 

示例2

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

# Form implementation generated from reading ui file 'D:\data\QtProject\test\test.ui'
#
# Created by: PyQt5 UI code generator 5.9
#
# WARNING! All changes made in this file will be lost!

from PyQt5 import QtCore, QtGui, QtWidgets

class Ui_Dialog(object):
    def setupUi(self, Dialog):
        Dialog.setObjectName("Dialog")
        Dialog.resize(582, 374)
        Dialog.setSizeGripEnabled(True)
        self.label = QtWidgets.QLabel(Dialog)
        self.label.setGeometry(QtCore.QRect(200, 90, 81, 18))
        self.label.setObjectName("label")
        self.pushButton = QtWidgets.QPushButton(Dialog)
        self.pushButton.setGeometry(QtCore.QRect(90, 230, 112, 34))
        self.pushButton.setObjectName("pushButton")
        self.pushButton_2 = QtWidgets.QPushButton(Dialog)
        self.pushButton_2.setGeometry(QtCore.QRect(300, 230, 112, 34))
        self.pushButton_2.setObjectName("pushButton_2")

        self.retranslateUi(Dialog)
        self.pushButton_2.clicked.connect(Dialog.close)
        QtCore.QMetaObject.connectSlotsByName(Dialog)

    def retranslateUi(self, Dialog):
        _translate = QtCore.QCoreApplication.translate
        Dialog.setWindowTitle(_translate("Dialog", "Dialog"))
        self.label.setText(_translate("Dialog", "显示"))
        self.pushButton.setText(_translate("Dialog", "确定"))
        self.pushButton_2.setText(_translate("Dialog", "关闭"))


if __name__ == "__main__":
    import sys
    app = QtWidgets.QApplication(sys.argv)
    Dialog = QtWidgets.QDialog()
    ui = Ui_Dialog()
    ui.setupUi(Dialog)
    Dialog.show()
    sys.exit(app.exec_()) 
eric环境生成的ython代码
# -*- coding: utf-8 -*-

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

from PyQt5 import QtCore, QtGui, QtWidgets

class Ui_Dialog(object):
    def setupUi(self, Dialog):
        Dialog.setObjectName("Dialog")
        Dialog.resize(582, 374)
        Dialog.setSizeGripEnabled(True)
        self.label = QtWidgets.QLabel(Dialog)
        self.label.setGeometry(QtCore.QRect(200, 90, 81, 18))
        self.label.setObjectName("label")
        self.pushButton = QtWidgets.QPushButton(Dialog)
        self.pushButton.setGeometry(QtCore.QRect(90, 230, 112, 34))
        self.pushButton.setObjectName("pushButton")
        self.pushButton_2 = QtWidgets.QPushButton(Dialog)
        self.pushButton_2.setGeometry(QtCore.QRect(300, 230, 112, 34))
        self.pushButton_2.setObjectName("pushButton_2")

        self.retranslateUi(Dialog)
        self.pushButton_2.clicked.connect(Dialog.close)
        QtCore.QMetaObject.connectSlotsByName(Dialog)

    def retranslateUi(self, Dialog):
        _translate = QtCore.QCoreApplication.translate
        Dialog.setWindowTitle(_translate("Dialog", "Dialog"))
        self.label.setText(_translate("Dialog", "显示"))
        self.pushButton.setText(_translate("Dialog", "确定"))
        self.pushButton_2.setText(_translate("Dialog", "关闭"))
python环境生成的代码(没有main)
# -*- coding: utf-8 -*-

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

from PyQt5 import QtCore, QtGui, QtWidgets

class Ui_Dialog(object):
    def setupUi(self, Dialog):
        Dialog.setObjectName("Dialog")
        Dialog.resize(582, 374)
        Dialog.setSizeGripEnabled(True)
        self.label = QtWidgets.QLabel(Dialog)
        self.label.setGeometry(QtCore.QRect(200, 90, 81, 18))
        self.label.setObjectName("label")
        self.pushButton = QtWidgets.QPushButton(Dialog)
        self.pushButton.setGeometry(QtCore.QRect(90, 230, 112, 34))
        self.pushButton.setObjectName("pushButton")
        self.pushButton_2 = QtWidgets.QPushButton(Dialog)
        self.pushButton_2.setGeometry(QtCore.QRect(300, 230, 112, 34))
        self.pushButton_2.setObjectName("pushButton_2")

        self.retranslateUi(Dialog)
        self.pushButton_2.clicked.connect(Dialog.close)
        QtCore.QMetaObject.connectSlotsByName(Dialog)

    def retranslateUi(self, Dialog):
        _translate = QtCore.QCoreApplication.translate
        Dialog.setWindowTitle(_translate("Dialog", "Dialog"))
        self.label.setText(_translate("Dialog", "显示"))
        self.pushButton.setText(_translate("Dialog", "确定"))
        self.pushButton_2.setText(_translate("Dialog", "关闭"))


class W(QtWidgets.QDialog, Ui_Dialog):  # 继承实现类和QDialog类,以后eric代码移植到pycharm只需要改这两处
    def __init__(self):
        super().__init__()
        self.setupUi(self)  # 调用Ui_Dialog类的方法


if __name__ == "__main__":
    import sys
    app = QtWidgets.QApplication(sys.argv)
    w = W()
    w.show()
    sys.exit(app.exec_())


#eric代码
# if __name__ == "__main__":
#     import sys
#     app = QtWidgets.QApplication(sys.argv)
#     Dialog = QtWidgets.QDialog()  # 这里放到W继承类
#     ui = Ui_Dialog()
#     ui.setupUi(Dialog)
#     Dialog.show()
#     sys.exit(app.exec_())
完善pycharm类代码

总结

eric有个问题: 智能提示很louw很难用(放弃eric编码)

PyCharm有三个问题:
    1.只有类代码没有调用代码(eric生成的python调用代码,复制到pycharm)   
    2.pycharm停止运行,extratool调用的Qt会关闭(手动启动Qt)
    3.pycharm每次把ui转换为py文件会覆盖上次内容(因此调用代码要和类代码分开,被调用代码放到包目录下)

总结:
    启动eric建立ui文件然后编译运行,复制ui文件和py调用代码分别到pycharm包目录和main.py
    手动启动Qt打开pycharm下的ui文件
    关闭eric

  

  

  

  

  

  

转载于:https://www.cnblogs.com/hyit/articles/7701998.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值