买菜计算器 PYQT5 UI练习

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

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

import sys
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtWidgets import QApplication, QWidget


class Ui_Form(object):
    def setupUi(self, Form):
        Form.setObjectName("Form")
        Form.resize(564, 386)
        self.gridLayout_4 = QtWidgets.QGridLayout(Form)
        self.gridLayout_4.setObjectName("gridLayout_4")
        self.groupBox = QtWidgets.QGroupBox(Form)
        font = QtGui.QFont()
        font.setPointSize(10)
        self.groupBox.setFont(font)
        self.groupBox.setObjectName("groupBox")
        self.gridLayout_5 = QtWidgets.QGridLayout(self.groupBox)
        self.gridLayout_5.setObjectName("gridLayout_5")
        self.label_shuliang = QtWidgets.QLabel(self.groupBox)
        self.label_shuliang.setObjectName("label_shuliang")
        self.gridLayout_5.addWidget(self.label_shuliang, 0, 0, 1, 1)
        self.lineEdit_shuliang = QtWidgets.QLineEdit(self.groupBox)
        self.lineEdit_shuliang.setObjectName("lineEdit_shuliang")
        self.gridLayout_5.addWidget(self.lineEdit_shuliang, 0, 1, 1, 1)
        self.label_danjia = QtWidgets.QLabel(self.groupBox)
        self.label_danjia.setObjectName("label_danjia")
        self.gridLayout_5.addWidget(self.label_danjia, 0, 2, 1, 1)
        self.lineEdit_danjia = QtWidgets.QLineEdit(self.groupBox)
        self.lineEdit_danjia.setObjectName("lineEdit_danjia")
        self.gridLayout_5.addWidget(self.lineEdit_danjia, 0, 3, 1, 1)
        self.label_zongjia = QtWidgets.QLabel(self.groupBox)
        self.label_zongjia.setObjectName("label_zongjia")
        self.gridLayout_5.addWidget(self.label_zongjia, 1, 2, 1, 1)
        self.lineEdit_zogjia = QtWidgets.QLineEdit(self.groupBox)
        self.lineEdit_zogjia.setObjectName("lineEdit_zogjia")
        self.gridLayout_5.addWidget(self.lineEdit_zogjia, 1, 3, 1, 1)
        self.pushButton_jisuang = QtWidgets.QPushButton(self.groupBox)
        self.pushButton_jisuang.setObjectName("pushButton_jisuang")
        self.gridLayout_5.addWidget(self.pushButton_jisuang, 1, 1, 1, 1)
        self.gridLayout_4.addWidget(self.groupBox, 0, 0, 1, 1)
        self.groupBox_2 = QtWidgets.QGroupBox(Form)
        font = QtGui.QFont()
        font.setPointSize(10)
        self.groupBox_2.setFont(font)
        self.groupBox_2.setFlat(False)
        self.groupBox_2.setCheckable(False)
        self.groupBox_2.setObjectName("groupBox_2")
        self.gridLayout = QtWidgets.QGridLayout(self.groupBox_2)
        self.gridLayout.setObjectName("gridLayout")
        self.spin_Lshuliang = QtWidgets.QLabel(self.groupBox_2)
        self.spin_Lshuliang.setObjectName("spin_Lshuliang")
        self.gridLayout.addWidget(self.spin_Lshuliang, 0, 0, 1, 1)
        self.spin_shuliang_2 = QtWidgets.QSpinBox(self.groupBox_2)
        self.spin_shuliang_2.setObjectName("spin_shuliang_2")
        self.gridLayout.addWidget(self.spin_shuliang_2, 0, 1, 1, 1)
        self.spin_Ldanjia = QtWidgets.QLabel(self.groupBox_2)
        self.spin_Ldanjia.setObjectName("spin_Ldanjia")
        self.gridLayout.addWidget(self.spin_Ldanjia, 0, 2, 1, 1)
        self.spin_danjia = QtWidgets.QSpinBox(self.groupBox_2)
        self.spin_danjia.setObjectName("spin_danjia")
        self.gridLayout.addWidget(self.spin_danjia, 0, 3, 1, 1)
        self.spin_Lzidong = QtWidgets.QLabel(self.groupBox_2)
        self.spin_Lzidong.setObjectName("spin_Lzidong")
        self.gridLayout.addWidget(self.spin_Lzidong, 1, 2, 1, 1)
        self.spin_zidongjishuan = QtWidgets.QSpinBox(self.groupBox_2)
        self.spin_zidongjishuan.setObjectName("spin_zidongjishuan")
        self.gridLayout.addWidget(self.spin_zidongjishuan, 1, 3, 1, 1)
        self.gridLayout_4.addWidget(self.groupBox_2, 1, 0, 1, 1)

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

    def retranslateUi(self, Form):
        _translate = QtCore.QCoreApplication.translate
        Form.setWindowTitle(_translate("Form", "买菜计算器"))
        self.groupBox.setTitle(_translate("Form", "LineEdit输入和显示数值"))
        self.label_shuliang.setText(_translate("Form", "数量"))
        self.label_danjia.setText(_translate("Form", "单价"))
        self.label_zongjia.setText(_translate("Form", "总价"))
        self.pushButton_jisuang.setText(_translate("Form", "计算总价"))
        self.groupBox_2.setTitle(_translate("Form", "Spin输入和显示"))
        self.spin_Lshuliang.setText(_translate("Form", "数量"))
        self.spin_Ldanjia.setText(_translate("Form", "单价"))
        self.spin_Lzidong.setText(_translate("Form", "自动计算总价"))


class JiSuan(QWidget):
    def __init__(self, parent=None):
        super().__init__(parent)
        self.ui = Ui_Form()
        self.ui.setupUi(self)
        self.ui.pushButton_jisuang.clicked.connect(self.do_btnjisuan)
        self.ui.spin_shuliang_2.valueChanged.connect(self.do_spinautocount)
        self.ui.spin_danjia.valueChanged.connect(self.do_spinautoprice)

    def do_btnjisuan(self):
        num = int(self.ui.lineEdit_shuliang.text())
        price = float(self.ui.lineEdit_danjia.text())
        if num==0 or price==0:
            self.ui.lineEdit_shuliang.setText(self,'1')
            self.ui.lineEdit_danjia
        total = num * price
        self.ui.lineEdit_zogjia.setText("%.2f" % total)

    def do_spinautocount(self, count):
        price = self.ui.spin_danjia.value()
        self.ui.spin_zidongjishuan.setValue(count * price)

    def do_spinautoprice(self, price):
        count = self.ui.spin_shuliang_2.value()
        self.ui.spin_zidongjishuan.setValue(count * price)


if __name__ == "__main__":
    app = QApplication(sys.argv)
    form = JiSuan()
    form.show()
    sys.exit(app.exec_())

 

posted @ 2022-01-24 19:59  再次路过之  阅读(80)  评论(0编辑  收藏  举报