QT Designer + python 编写小程序并打包成exe

编写一个计算税率小程序,QT Designer是设计界面的,使用起来跟用工业中的触摸屏差不多,都是组态的,界面设计如下

QT Designer + python 编写小程序并打包成exe

有用到的控件要取一个比较有意义的名字,设计完后会生成一个.ui的文件,其代码如下(用Designer编写比直接敲代码方便多了)

     </font>
    </property>
    <property name="text">
     <string>税率计算器</string>
    </property>
   </widget>
   <widget class="QTextEdit" name="PriceBox">
    <property name="geometry">
     <rect>
      <x>130</x>
      <y>80</y>
      <width>101</width>
      <height>31</height>
     </rect>
    </property>
   </widget>
   <widget class="QLabel" name="label_2">
    <property name="geometry">
     <rect>
      <x>50</x>
      <y>80</y>
      <width>54</width>
      <height>21</height>
     </rect>
    </property>
    <property name="font">
     <font>
      <family>AcadEref</family>
      <pointsize>14</pointsize>
     </font>
    </property>
    <property name="text">
     <string>价格</string>
    </property>
   </widget>
   <widget class="QSpinBox" name="TaxBox">
    <property name="geometry">
     <rect>
      <x>130</x>
      <y>130</y>
      <width>101</width>
      <height>31</height>
     </rect>
    </property>
    <property name="value">
     <number>20</number>
    </property>
   </widget>
   <widget class="QLabel" name="label_3">
    <property name="geometry">
     <rect>
      <x>50</x>
      <y>130</y>
      <width>54</width>
      <height>21</height>
     </rect>
    </property>
    <property name="font">
     <font>
      <family>AcadEref</family>
      <pointsize>14</pointsize>
     </font>
    </property>
    <property name="text">
     <string>税率</string>
    </property>
   </widget>
   <widget class="QPushButton" name="CalcButton">
    <property name="geometry">
     <rect>
      <x>130</x>
      <y>170</y>
      <width>101</width>
      <height>31</height>
     </rect>
    </property>
    <property name="text">
     <string>计算</string>
    </property>
   </widget>
   <widget class="QTextEdit" name="ResultWindow">
    <property name="geometry">
     <rect>
      <x>60</x>
      <y>210</y>
      <width>171</width>
      <height>71</height>
     </rect>
    </property>
   </widget>
  </widget>
  <widget class="QMenuBar" name="menubar">
   <property name="geometry">
    <rect>
     <x>0</x>
     <y>0</y>
     <width>563</width>
     <height>23</height>
    </rect>
   </property>
  </widget>
  <widget class="QStatusBar" name="statusbar"/>
 </widget>
 <resources/>
 <connections/>
</ui>

.ui文件要转换成.py文件,打开Anaconda Prompt,cd到ui所在文件夹,输入命令“pyuic5 -o 文件名.py 文件名.ui”,便转换好了。转换后代码如下(文件名:CalcTax.py)

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

# Form implementation generated from reading ui file 'CalcTax.ui'
#
# Created by: PyQt5 UI code generator 5.6
#
# 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(563, 349)
        self.centralwidget = QtWidgets.QWidget(MainWindow)
        self.centralwidget.setObjectName("centralwidget")
        self.label = QtWidgets.QLabel(self.centralwidget)
        self.label.setGeometry(QtCore.QRect(200, 10, 101, 21))
        font = QtGui.QFont()
        font.setFamily("AcadEref")
        font.setPointSize(14)
        self.label.setFont(font)
        self.label.setObjectName("label")
        self.PriceBox = QtWidgets.QTextEdit(self.centralwidget)
        self.PriceBox.setGeometry(QtCore.QRect(130, 80, 101, 31))
        self.PriceBox.setObjectName("PriceBox")
        self.label_2 = QtWidgets.QLabel(self.centralwidget)
        self.label_2.setGeometry(QtCore.QRect(50, 80, 54, 21))
        font = QtGui.QFont()
        font.setFamily("AcadEref")
        font.setPointSize(14)
        self.label_2.setFont(font)
        self.label_2.setObjectName("label_2")
        self.TaxBox = QtWidgets.QSpinBox(self.centralwidget)
        self.TaxBox.setGeometry(QtCore.QRect(130, 130, 101, 31))
        self.TaxBox.setProperty("value", 20)
        self.TaxBox.setObjectName("TaxBox")
        self.label_3 = QtWidgets.QLabel(self.centralwidget)
        self.label_3.setGeometry(QtCore.QRect(50, 130, 54, 21))
        font = QtGui.QFont()
        font.setFamily("AcadEref")
        font.setPointSize(14)
        self.label_3.setFont(font)
        self.label_3.setObjectName("label_3")
        self.CalcButton = QtWidgets.QPushButton(self.centralwidget)
        self.CalcButton.setGeometry(QtCore.QRect(130, 170, 101, 31))
        self.CalcButton.setObjectName("CalcButton")
        self.ResultWindow = QtWidgets.QTextEdit(self.centralwidget)
        self.ResultWindow.setGeometry(QtCore.QRect(60, 210, 171, 71))
        self.ResultWindow.setObjectName("ResultWindow")
        MainWindow.setCentralWidget(self.centralwidget)
        self.menubar = QtWidgets.QMenuBar(MainWindow)
        self.menubar.setGeometry(QtCore.QRect(0, 0, 563, 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", "税率计算器"))
        self.label_2.setText(_translate("MainWindow", "价格"))
        self.label_3.setText(_translate("MainWindow", "税率"))
        self.CalcButton.setText(_translate("MainWindow", "计算"))

然后编写python文件,python代码如下:

#!/usr/bin/env python
# -*- coding: utf-8 -*-
import sys

from PyQt5.QtWidgets import QApplication, QMainWindow 
from CalcTax import Ui_MainWindow
 

 
class MyApp(Ui_MainWindow,QMainWindow):
    def __init__(self):
        super(MyApp,self).__init__()
        self.setupUi(self)
        self.CalcButton.clicked.connect(self.CalcuateTax)
    def CalcuateTax(self):
        price = int(self.PriceBox.toPlainText())
        tax = (self.TaxBox.value())
        total_price = price + ((tax/100) * price)
        total_price_string = "税费是:" + str(total_price)
        self.ResultWindow.setText(total_price_string)
if __name__ == "__main__":
    app = QApplication(sys.argv)
    window = MyApp()
    window.show()
    sys.exit(app.exec_())

生成exe文件:安装pyinstaller,用pyinstaller生成exe文件,命令如下

pyinstaller -F -i mylogo.ico 11.py CalcTax.py --noconsole

-F表示只生成一个exe文件,-i表示指定exe图标,mylogo.ico是图片文件,ico格式可在比特虫网站生成,11.py 是主文件,CalcTax.py是被调用的文件。 --noconsole表示不显示控制台。生成的exe在dist文件夹下,要把C:\ProgramData\Anaconda3\Library\plugins\platforms整个文件夹拷贝到exe同个文件夹下,不然会报错。