PyQt5: chapter2-使用数字调整框小部件

QSpinBox类

  • value()
  • text()
  • setPrefix()
  • setSuffix()
  • cleanText()
  • setValue()
  • setSingleStep():此方法设置数字调整框的步长。步长是数字调整框的增量/减量值,即选择向上或向下按钮时数字调整框的值将增加或减少的值。
  • setMinimum()
  • setMaximum()
  • setWrapping():此方法将布尔值true传递给此方法以在数字调整框中启用换行。换行是指在显示最大值时按向上按钮时,数字调整框返回到第一个值(最小值)。

QSpinBox 类

  • valueChanged()
  • editingFinished()

实例

  1. 创建Dialog without Buttons模板窗口
  2. 拖出三个Label部件,一个Spin Box部件,一个Double Box部件和四个Line Edit部件
  3. 设定前两个Label部件的text属性为Book Price value,Sugar Price,第三个Label部件的object Name属性设为labelTotalAmount
  4. 设定四个Line Edit部件的objectName属性为lineEditBookPrice, lineEditBookAmount, lineEditSugarPrice, lineEditSugarAmount
  5. 设定SpinBox的objectName属性为spinBoxBookQty,设定DoubleSpinBox部件的object Name属性设为doubleSpinBoxSugarWeight
  6. 删除第三个Label部件的text属性内容
  7. 设定两Line Edit部件(lineEditBookAmount,lineEditSugarAmount)为不可编辑,即在属性编辑器中取消选中可编辑状态
  8. 保存为demoSpinner.ui文件
  9. 使用pyuic生成demoSpinner.py文件
  10. 创建calldemoSpinner.py文件,代码如下
import sys
from PyQt5.QtWidgets import QDialog,QApplication
from cookbook_200420.demoSpinner import *

class MyForm(QDialog):
    def __init__(self):
        super().__init__()
        self.ui=Ui_Dialog()
        self.ui.setupUi(self)
        self.ui.spinBoxBookQty.editingFinished.connect(self.result1)
        self.ui.doubleSpinBoxSugarWeight.editingFinished.connect(self.result2)
        self.show()
    def result1(self):
        bookPrice=0
        totalBookAmount=0
        if len(self.ui.lineEditBookPrice.text())!=0:
            bookPrice=int(self.ui.lineEditBookPrice.text())
        totalBookAmount=self.ui.spinBoxBookQty.value()*bookPrice
        self.ui.lineEditBookAmount.setText(str(totalBookAmount))
    def result2(self):
        sugarPrice = 0
        totalSugarAmount=0
        totalAmount=0
        if len(self.ui.lineEditSugarPrice.text())!=0:
            sugarPrice=float(self.ui.lineEditSugarPrice.text())
        totalSugarAmount=self.ui.doubleSpinBoxSugarWeight.value()*sugarPrice
        self.ui.lineEditSugarAmount.setText(str(round(totalSugarAmount,2)))
        totalBookAmount=int(self.ui.lineEditBookAmount.text())
        totalAmount=totalBookAmount+totalSugarAmount
        self.ui.labelTotalAmount.setText(str(round(totalAmount,2)))
if __name__=="__main__":
    app=QApplication(sys.argv)
    w=MyForm()
    w.show()
    sys.exit(app.exec())

demospinner.py

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

# Form implementation generated from reading ui file 'demoSpinner.ui'
#
# Created by: PyQt5 UI code generator 5.13.0
#
# 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(400, 300)
        self.label = QtWidgets.QLabel(Dialog)
        self.label.setGeometry(QtCore.QRect(13, 52, 101, 21))
        self.label.setObjectName("label")
        self.label_2 = QtWidgets.QLabel(Dialog)
        self.label_2.setGeometry(QtCore.QRect(13, 131, 101, 21))
        self.label_2.setObjectName("label_2")
        self.labelTotalAmount = QtWidgets.QLabel(Dialog)
        self.labelTotalAmount.setGeometry(QtCore.QRect(233, 222, 101, 20))
        self.labelTotalAmount.setText("")
        self.labelTotalAmount.setObjectName("labelTotalAmount")
        self.lineEditBookPrice = QtWidgets.QLineEdit(Dialog)
        self.lineEditBookPrice.setGeometry(QtCore.QRect(120, 50, 51, 21))
        self.lineEditBookPrice.setObjectName("lineEditBookPrice")
        self.lineEditSugarPrice = QtWidgets.QLineEdit(Dialog)
        self.lineEditSugarPrice.setGeometry(QtCore.QRect(120, 130, 51, 21))
        self.lineEditSugarPrice.setObjectName("lineEditSugarPrice")
        self.lineEditBookAmount = QtWidgets.QLineEdit(Dialog)
        self.lineEditBookAmount.setEnabled(True)
        self.lineEditBookAmount.setGeometry(QtCore.QRect(280, 50, 51, 21))
        self.lineEditBookAmount.setText("")
        self.lineEditBookAmount.setReadOnly(True)
        self.lineEditBookAmount.setObjectName("lineEditBookAmount")
        self.spinBoxBookQty = QtWidgets.QSpinBox(Dialog)
        self.spinBoxBookQty.setGeometry(QtCore.QRect(190, 50, 61, 21))
        self.spinBoxBookQty.setObjectName("spinBoxBookQty")
        self.doubleSpinBoxSugarWeight = QtWidgets.QDoubleSpinBox(Dialog)
        self.doubleSpinBoxSugarWeight.setGeometry(QtCore.QRect(190, 130, 62, 22))
        self.doubleSpinBoxSugarWeight.setObjectName("doubleSpinBoxSugarWeight")
        self.lineEditSugarAmount = QtWidgets.QLineEdit(Dialog)
        self.lineEditSugarAmount.setEnabled(True)
        self.lineEditSugarAmount.setGeometry(QtCore.QRect(280, 130, 51, 21))
        self.lineEditSugarAmount.setText("")
        self.lineEditSugarAmount.setReadOnly(True)
        self.lineEditSugarAmount.setObjectName("lineEditSugarAmount")

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

    def retranslateUi(self, Dialog):
        _translate = QtCore.QCoreApplication.translate
        Dialog.setWindowTitle(_translate("Dialog", "Dialog"))
        self.label.setText(_translate("Dialog", "Book Price value "))
        self.label_2.setText(_translate("Dialog", "Sugar Price"))

PyQt5: chapter2-使用数字调整框小部件_第1张图片PyQt5: chapter2-使用数字调整框小部件_第2张图片

你可能感兴趣的:(PyQt5笔记)