Price * (1 - discount)
每当我输入数据时,如何自动计算净价 ( )?我可以让它在按下按钮时工作,但我想在输入数据时这样做。
见下图。
先感谢您!
起初,您似乎在使用 QLineEdit 作为输入。因为你想输入整数,我建议使用 QSpinBox 或 QDoubleSpinBox,它们是为输入整数而设计的。
我用 QtCreator 构建了这样一个 GUI,并pyuic5 -x gui.ui -o gui.py
在我的 Raspberry Pi上将其转换为 Python。
您只需将 SpinBox 的价值变化与给出净价格的函数联系起来。为此,请参阅以下代码:
# -*- coding: utf-8 -*-
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(800, 415)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.label = QtWidgets.QLabel(self.centralwidget)
self.label.setGeometry(QtCore.QRect(30, 40, 201, 81))
font = QtGui.QFont()
font.setPointSize(26)
self.label.setFont(font)
self.label.setObjectName("label")
self.label_2 = QtWidgets.QLabel(self.centralwidget)
self.label_2.setGeometry(QtCore.QRect(30, 140, 201, 81))
font = QtGui.QFont()
font.setPointSize(26)
self.label_2.setFont(font)
self.label_2.setObjectName("label_2")
self.label_3 = QtWidgets.QLabel(self.centralwidget)
self.label_3.setGeometry(QtCore.QRect(30, 260, 201, 81))
font = QtGui.QFont()
font.setPointSize(26)
self.label_3.setFont(font)
self.label_3.setObjectName("label_3")
self.label_netprice = QtWidgets.QLabel(self.centralwidget)
self.label_netprice.setGeometry(QtCore.QRect(260, 282, 421, 41))
self.label_netprice.setStyleSheet("background-color: rgb(255, 255, 255);")
self.label_netprice.setText("")
self.label_netprice.setObjectName("label_netprice")
self.doubleSpinBox_price = QtWidgets.QDoubleSpinBox(self.centralwidget)
self.doubleSpinBox_price.setGeometry(QtCore.QRect(260, 60, 421, 41))
self.doubleSpinBox_price.setObjectName("doubleSpinBox_price")
####################################
# Link to function when value changes
self.doubleSpinBox_price.valueChanged.connect(self.Refresh)
####################################
self.doubleSpinBox_discount = QtWidgets.QDoubleSpinBox(self.centralwidget)
self.doubleSpinBox_discount.setGeometry(QtCore.QRect(260, 160, 421, 41))
self.doubleSpinBox_discount.setObjectName("doubleSpinBox_discount")
####################################
# Link to function when value changes
self.doubleSpinBox_discount.valueChanged.connect(self.Refresh)
####################################
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 800, 21))
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", "Price:"))
self.label_2.setText(_translate("MainWindow", "Discount:"))
self.label_3.setText(_translate("MainWindow", "Net Price:"))
##########################################
# Function for refreshing the output label
def Refresh(self):
price = self.doubleSpinBox_price.value()
discount = self.doubleSpinBox_discount.value()
netprice = price * (1 - discount)
self.label_netprice.setText(str(netprice))
##########################################
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
MainWindow = QtWidgets.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())
在我的代码中,SpinBoxes 限制为 99,但您可以将其设置为更高的值。
只是一个提示:人们更有可能帮助你,如果你给他们一个简短但相关的代码示例来使用并且他们不必构建自己的 GUI。
我希望这有帮助。如果您觉得有用,请标记为正确。
最好的祝愿,
拉斯皮马努
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句