程序人生
PyQt5改造计算脚本示例及打包
作者:邵波涛    时间:2016-12-16 浏览量:249

脚本改造过程很简单,把原始脚本中需要input输入部分转为输入部件录入即可。

1、先看看原始脚本(计算公式已隐去)

#-------------------------------------------------------------------------------
# Name:        calc
# Purpose:
#
# Author:      shaobotao
#
# Created:     11-12-2015
# Copyright:   (c) shaobotao 2015
# Licence:     <sbtwork@126.com>
#-------------------------------------------------------------------------------

import math
PI=3.1415
def max(a,b) :
    if a>b :
        return a
    else:
        return b
j1=float(input("请输入转向桥内转角值(较大):"))
j2=float(input("请输入转向桥外转角值(较小):"))
o=float(input("请输入转向主销距:"))
l=float(input("请输入轴距:"))
m=float(input("请输入驱动轮距:"))
r1= *******
print("内转角对应内前轮距旋转中心距离为:"+ str(r1))
r2=*******
print("外转角对应内前轮距旋转中心距离为:"+ str(r2))
r=max(r1,r2)
print("计算取较大值:"+ str(r))
b=float(input("请输入距离旋转中心最远点处的车辆宽度:"))
s=float(input("请输入最远点处距驱动轴的距离:"))
Rmin=*******
print("===========================================最小转弯半径为:"+ str(Rmin))
bg=float(input("请输入货物长度(前后方向):"))
lg=float(input("请输入货物宽度(左右方向):"))
a=float(input("请输入前悬:"))
et=float(input("请输入失载距:"))
print("通过间隙,依司机熟练程度取100~300之间")
s0=float(input("请输入通过间隙:"))
if lg<2*(bg+a+et) :
    width=*******
else :
    width=*******
print("===========================================堆垛通道宽度为:"+str(width))
Ba=*******
print("货物方向通道宽度为:"+ str(Ba))
Bb=*******
print("车身方向通道宽度为:"+ str(Bb))
B=max(Ba,Bb)
print("===========================================最小直角通道宽度为:"+str(B))
input("==回车结束==")

看看原始界面:

2、使用PyQt制作UI,Ui_Calc.py代码如下:

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

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

from PyQt5 import QtCore, QtGui, QtWidgets

class Ui_Calcui(object):
    def setupUi(self, Calcui):
        Calcui.setObjectName("Calcui")
        Calcui.resize(381, 429)
        Calcui.setMinimumSize(QtCore.QSize(381, 429))
        Calcui.setMaximumSize(QtCore.QSize(381, 429))
        Calcui.setSizeGripEnabled(True)
        self.gridLayoutWidget = QtWidgets.QWidget(Calcui)
        self.gridLayoutWidget.setGeometry(QtCore.QRect(10, 10, 361, 401))
        self.gridLayoutWidget.setObjectName("gridLayoutWidget")
        self.gridLayout = QtWidgets.QGridLayout(self.gridLayoutWidget)
        self.gridLayout.setContentsMargins(0, 0, 0, 0)
        self.gridLayout.setObjectName("gridLayout")
        self.A = QtWidgets.QLineEdit(self.gridLayoutWidget)
        self.A.setObjectName("A")
        self.gridLayout.addWidget(self.A, 10, 2, 1, 1)
        self.label_3 = QtWidgets.QLabel(self.gridLayoutWidget)
        self.label_3.setObjectName("label_3")
        self.gridLayout.addWidget(self.label_3, 2, 1, 1, 1)
        self.J1 = QtWidgets.QLineEdit(self.gridLayoutWidget)
        self.J1.setObjectName("J1")
        self.gridLayout.addWidget(self.J1, 0, 2, 1, 1)
        spacerItem = QtWidgets.QSpacerItem(20, 40, QtWidgets.QSizePolicy.Minimum, QtWidgets.QSizePolicy.Expanding)
        self.gridLayout.addItem(spacerItem, 15, 1, 1, 1)
        self.label_4 = QtWidgets.QLabel(self.gridLayoutWidget)
        self.label_4.setObjectName("label_4")
        self.gridLayout.addWidget(self.label_4, 3, 1, 1, 1)
        self.label_15 = QtWidgets.QLabel(self.gridLayoutWidget)
        self.label_15.setStyleSheet("color: rgb(85, 0, 255);")
        self.label_15.setObjectName("label_15")
        self.gridLayout.addWidget(self.label_15, 13, 1, 1, 1)
        self.LG = QtWidgets.QLineEdit(self.gridLayoutWidget)
        self.LG.setObjectName("LG")
        self.gridLayout.addWidget(self.LG, 9, 2, 1, 1)
        self.M = QtWidgets.QLineEdit(self.gridLayoutWidget)
        self.M.setObjectName("M")
        self.gridLayout.addWidget(self.M, 4, 2, 1, 1)
        self.O = QtWidgets.QLineEdit(self.gridLayoutWidget)
        self.O.setObjectName("O")
        self.gridLayout.addWidget(self.O, 2, 2, 1, 1)
        self.label_5 = QtWidgets.QLabel(self.gridLayoutWidget)
        self.label_5.setObjectName("label_5")
        self.gridLayout.addWidget(self.label_5, 4, 1, 1, 1)
        self.R = QtWidgets.QLabel(self.gridLayoutWidget)
        self.R.setStyleSheet("color: rgb(85, 0, 255);")
        self.R.setText("")
        self.R.setObjectName("R")
        self.gridLayout.addWidget(self.R, 7, 2, 1, 1)
        self.J2 = QtWidgets.QLineEdit(self.gridLayoutWidget)
        self.J2.setObjectName("J2")
        self.gridLayout.addWidget(self.J2, 1, 2, 1, 1)
        self.label = QtWidgets.QLabel(self.gridLayoutWidget)
        self.label.setObjectName("label")
        self.gridLayout.addWidget(self.label, 0, 1, 1, 1)
        self.label_6 = QtWidgets.QLabel(self.gridLayoutWidget)
        self.label_6.setObjectName("label_6")
        self.gridLayout.addWidget(self.label_6, 5, 1, 1, 1)
        self.label_2 = QtWidgets.QLabel(self.gridLayoutWidget)
        self.label_2.setObjectName("label_2")
        self.gridLayout.addWidget(self.label_2, 1, 1, 1, 1)
        self.L = QtWidgets.QLineEdit(self.gridLayoutWidget)
        self.L.setObjectName("L")
        self.gridLayout.addWidget(self.L, 3, 2, 1, 1)
        self.label_8 = QtWidgets.QLabel(self.gridLayoutWidget)
        self.label_8.setStyleSheet("color: rgb(85, 0, 255);")
        self.label_8.setObjectName("label_8")
        self.gridLayout.addWidget(self.label_8, 7, 1, 1, 1)
        self.S = QtWidgets.QLineEdit(self.gridLayoutWidget)
        self.S.setObjectName("S")
        self.gridLayout.addWidget(self.S, 6, 2, 1, 1)
        self.label_16 = QtWidgets.QLabel(self.gridLayoutWidget)
        self.label_16.setStyleSheet("color: rgb(85, 0, 255);")
        self.label_16.setObjectName("label_16")
        self.gridLayout.addWidget(self.label_16, 14, 1, 1, 1)
        self.label_7 = QtWidgets.QLabel(self.gridLayoutWidget)
        self.label_7.setObjectName("label_7")
        self.gridLayout.addWidget(self.label_7, 6, 1, 1, 1)
        self.B = QtWidgets.QLineEdit(self.gridLayoutWidget)
        self.B.setObjectName("B")
        self.gridLayout.addWidget(self.B, 5, 2, 1, 1)
        self.BG = QtWidgets.QLineEdit(self.gridLayoutWidget)
        self.BG.setObjectName("BG")
        self.gridLayout.addWidget(self.BG, 8, 2, 1, 1)
        self.label_10 = QtWidgets.QLabel(self.gridLayoutWidget)
        self.label_10.setObjectName("label_10")
        self.gridLayout.addWidget(self.label_10, 9, 1, 1, 1)
        self.label_9 = QtWidgets.QLabel(self.gridLayoutWidget)
        self.label_9.setObjectName("label_9")
        self.gridLayout.addWidget(self.label_9, 8, 1, 1, 1)
        self.label_13 = QtWidgets.QLabel(self.gridLayoutWidget)
        self.label_13.setObjectName("label_13")
        self.gridLayout.addWidget(self.label_13, 11, 1, 1, 1)
        self.S0 = QtWidgets.QLineEdit(self.gridLayoutWidget)
        self.S0.setObjectName("S0")
        self.gridLayout.addWidget(self.S0, 12, 2, 1, 1)
        self.label_11 = QtWidgets.QLabel(self.gridLayoutWidget)
        self.label_11.setObjectName("label_11")
        self.gridLayout.addWidget(self.label_11, 10, 1, 1, 1)
        self.label_14 = QtWidgets.QLabel(self.gridLayoutWidget)
        self.label_14.setObjectName("label_14")
        self.gridLayout.addWidget(self.label_14, 12, 1, 1, 1)
        self.W = QtWidgets.QLabel(self.gridLayoutWidget)
        self.W.setStyleSheet("color: rgb(85, 0, 255);")
        self.W.setText("")
        self.W.setObjectName("W")
        self.gridLayout.addWidget(self.W, 13, 2, 1, 1)
        self.WB = QtWidgets.QLabel(self.gridLayoutWidget)
        self.WB.setStyleSheet("color: rgb(85, 0, 255);")
        self.WB.setText("")
        self.WB.setObjectName("WB")
        self.gridLayout.addWidget(self.WB, 14, 2, 1, 1)
        spacerItem1 = QtWidgets.QSpacerItem(20, 40, QtWidgets.QSizePolicy.Minimum, QtWidgets.QSizePolicy.Expanding)
        self.gridLayout.addItem(spacerItem1, 15, 2, 1, 1)
        self.ET = QtWidgets.QLineEdit(self.gridLayoutWidget)
        self.ET.setObjectName("ET")
        self.gridLayout.addWidget(self.ET, 11, 2, 1, 1)
        self.pushButton = QtWidgets.QPushButton(Calcui)
        self.pushButton.setGeometry(QtCore.QRect(100, 400, 168, 23))
        self.pushButton.setObjectName("pushButton")

        self.retranslateUi(Calcui)
        QtCore.QMetaObject.connectSlotsByName(Calcui)
        Calcui.setTabOrder(self.J1, self.J2)
        Calcui.setTabOrder(self.J2, self.O)
        Calcui.setTabOrder(self.O, self.L)
        Calcui.setTabOrder(self.L, self.M)
        Calcui.setTabOrder(self.M, self.B)
        Calcui.setTabOrder(self.B, self.S)
        Calcui.setTabOrder(self.S, self.BG)
        Calcui.setTabOrder(self.BG, self.LG)
        Calcui.setTabOrder(self.LG, self.A)
        Calcui.setTabOrder(self.A, self.ET)
        Calcui.setTabOrder(self.ET, self.S0)
        Calcui.setTabOrder(self.S0, self.pushButton)

    def retranslateUi(self, Calcui):
        _translate = QtCore.QCoreApplication.translate
        Calcui.setWindowTitle(_translate("Calcui", "通过性计算"))
        self.label_3.setText(_translate("Calcui", "主销距:"))
        self.label_4.setText(_translate("Calcui", "轴距:"))
        self.label_15.setText(_translate("Calcui", "堆垛通道宽度:"))
        self.label_5.setText(_translate("Calcui", "驱动轮距:"))
        self.label.setText(_translate("Calcui", "转向桥内转角值(较大):"))
        self.label_6.setText(_translate("Calcui", "离转弯中心最远点处车体宽度:"))
        self.label_2.setText(_translate("Calcui", "转向桥内外角值(较小):"))
        self.label_8.setText(_translate("Calcui", "最小转弯半径:"))
        self.label_16.setText(_translate("Calcui", "最小直角通道宽度:"))
        self.label_7.setText(_translate("Calcui", "最远点距前桥距离:"))
        self.label_10.setText(_translate("Calcui", "货物宽度(左右):"))
        self.label_9.setText(_translate("Calcui", "货物长度(前后):"))
        self.label_13.setText(_translate("Calcui", "失载距:"))
        self.S0.setText(_translate("Calcui", "200"))
        self.label_11.setText(_translate("Calcui", "前悬:"))
        self.label_14.setText(_translate("Calcui", "通过间隙(100~300):"))
        self.pushButton.setText(_translate("Calcui", "计算"))


if __name__ == "__main__":
    import sys
    app = QtWidgets.QApplication(sys.argv)
    Calcui = QtWidgets.QDialog()
    ui = Ui_Calcui()
    ui.setupUi(Calcui)
    Calcui.show()
    sys.exit(app.exec_())

3、计算代码Calc.py代码如下:(计算公式已隐去)

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

"""
Module implementing Calc.
"""

from PyQt5.QtCore import pyqtSlot
from PyQt5.QtWidgets import QDialog, QApplication
from Ui_Calc import Ui_Calcui
import math,sys
PI=3.1415
def max(a,b) :
    if a>b :
        return a
    else:
        return b

class Calc(QDialog, Ui_Calcui):
    """
    Class documentation goes here.
    """
    def __init__(self, parent=None):
        """
        Constructor
        
        @param parent reference to the parent widget
        @type QWidget
        """
        super(Calc, self).__init__(parent)
        self.setupUi(self)
    
    @pyqtSlot()
    def on_pushButton_clicked(self):
        """
        Slot documentation goes here.
        """
        j1=float(self.J1.text())
        j2=float(self.J2.text())
        o=float(self.O.text())
        l=float(self.L.text())
        m=float(self.M.text())
        r1= ******       
        r2=******       
        r=max(r1,r2)        
        b=float(self.B.text())
        s=float(self.S.text())
        Rmin=******
        self.R.setText(str(Rmin))
        bg=float(self.BG.text())
        lg=float(self.LG.text())
        a=float(self.A.text())
        et=float(self.ET.text())        
        s0=float(self.S0.text())
        if lg<2*(bg+a+et) :
            width=******
        else :
            width=******
        self.W.setText(str(width))
        Ba=******       
        Bb=******        
        B=max(Ba,Bb)
        self.WB.setText(str(B))        
if __name__ == "__main__":    
    app = QApplication(sys.argv)
    dlg = Calc()     
    dlg.show()
    sys.exit(app.exec_())

好了执行成功。

最后一步打包为单文件EXE格式,因为本人系统同时安装有QT5和PyQt5,导致打包后报错:

pyinstaller failed to execute script pyi_rth_qt5plugins

因此打包时需指定qt库文件目录,但目录格式需使用linux的形式,命令如下:

pyinstaller --paths C:/Python35/Lib/site-packages/PyQt5/Qt/bin -F -w Calc.py


发言请留称呼=>
本文留言
共有 0 条留言
扫一扫,手机浏览网站
友情链接
申请链接