Linux自带Python3

PyCharm安装方法自行百度,这里就不多说了

首先安装PyQt5,Qt_Designer

终端输入命令 sudo pip3 install PyQt5  

                       sudo apt-get install qt5-default qttools5-dev-tools

安装好后,进入PyCharm进行配置,新建一个工程(路径最好不要有中文)然后进入External Tool添加下面两个集成工具

$JDKPath$
-m PyQt5.uic.pyuic $FileName$ -o $FileNameWithoutExtension$.py 
$FileDir$

/usr/lib/x86_64-linux-gnu/qt5/bin/designer   #designer的安装路径
$FileName$
$FileDir$

添加好了在Tools就可以看到

点击Qt5-Designer进入UI设计界面

此时PyCharm里就可以看到qt_1.ui文件,选择qt_1.ui用QtUIC工具转成.py文件

此时生成的qt_1.py

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

# Form implementation generated from reading ui file 'qt_1.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_dcdc(object):
    def setupUi(self, dcdc):
        dcdc.setObjectName("dcdc")
        dcdc.resize(377, 349)
        self.verticalLayout = QtWidgets.QVBoxLayout(dcdc)
        self.verticalLayout.setObjectName("verticalLayout")
        self.label = QtWidgets.QLabel(dcdc)
        self.label.setObjectName("label")
        self.verticalLayout.addWidget(self.label)
        self.pushButton_2 = QtWidgets.QPushButton(dcdc)
        self.pushButton_2.setObjectName("pushButton_2")
        self.verticalLayout.addWidget(self.pushButton_2)
        self.pushButton = QtWidgets.QPushButton(dcdc)
        self.pushButton.setObjectName("pushButton")
        self.verticalLayout.addWidget(self.pushButton)

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

    def retranslateUi(self, dcdc):
        _translate = QtCore.QCoreApplication.translate
        dcdc.setWindowTitle(_translate("dcdc", "Form"))
        self.label.setText(_translate("dcdc", "6"))
        self.pushButton_2.setText(_translate("dcdc", "2"))
        self.pushButton.setText(_translate("dcdc", "1"))


因为这是一个类,没有调用,所以运行没有什么现象,而且如果更改ui设计后,此py文件将重写,所以要新建一个代码文件main.py继承此文件功能,这样在就不用担心ui文件改变后,每次都要备份py文件的代码了。

from PyQt5 import QtCore, QtGui, QtWidgets
from qt_1 import Ui_dcdc
import sys


class My_Qt(Ui_dcdc):
    def __init__(self):
        pass

    def setupUi(self, dcdc):
        super().setupUi(dcdc)  #继承父类功能
        self.a = 0
        self.pushButton.clicked.connect(self.Add)
        self.pushButton_2.clicked.connect(self.Less)

    def Add(self):
        self.a += 1
        self.label.setText(str(self.a))
        print(self.a)

    def Less(self):
        self.a -= 1
        self.label.setText(str(self.a))
        print(self.a)



if __name__ == "__main__":
    app = QtWidgets.QApplication(sys.argv)
    widget = QtWidgets.QWidget()
    ui = My_Qt()
    ui.setupUi(widget)
    widget.show()
    sys.exit(app.exec_())


Logo

更多推荐