Checkbox 如何仅为pyqt5中的选定复选框运行代码?
我正在尝试使用pyqt5制作一个菜单GUI,菜单包括饮料和其他东西。 每个菜单项前面都有一个复选框,选中该复选框后,其价格将添加到账单中Checkbox 如何仅为pyqt5中的选定复选框运行代码?,checkbox,pyqt5,Checkbox,Pyqt5,我正在尝试使用pyqt5制作一个菜单GUI,菜单包括饮料和其他东西。 每个菜单项前面都有一个复选框,选中该复选框后,其价格将添加到账单中 self.latte_box = QtWidgets.QCheckBox(self.horizontalLayoutWidget) #latte_box is the name of the checkbox self.latte_box.setText("") self.latte_box.setObjectName("la
self.latte_box = QtWidgets.QCheckBox(self.horizontalLayoutWidget) #latte_box is the name of the checkbox
self.latte_box.setText("")
self.latte_box.setObjectName("latte_box")
self.horizontalLayout.addWidget(self.latte_box)
self.latte_box.stateChanged.connect(self.order)
self.cookie_box = QtWidgets.QCheckBox(self.horizontalLayoutWidget_3)
self.cookie_box.setText("")
self.cookie_box.setObjectName("cookie_box")
self.horizontalLayout_3.addWidget(self.cookie_box)
self.cookie_box.stateChanged.connect(self.order)
bill = 0 #the bill variable
def order(self):
if self.latte_box.isChecked():
bill += 2.85
else:
bill -= 2.85
if self.cookie_box.isChecked():
bill += 1.50
else:
bill -= 1.50
拿铁盒和饼干盒是清单上两个项目的复选框,价格分别为2.85美元和1.50美元。
因此,当用户选中该框时,项目的价格将添加到账单中,但如果出现错误,用户将取消选中该框,项目的价格将从账单中删除
self.latte_box = QtWidgets.QCheckBox(self.horizontalLayoutWidget) #latte_box is the name of the checkbox
self.latte_box.setText("")
self.latte_box.setObjectName("latte_box")
self.horizontalLayout.addWidget(self.latte_box)
self.latte_box.stateChanged.connect(self.order)
self.cookie_box = QtWidgets.QCheckBox(self.horizontalLayoutWidget_3)
self.cookie_box.setText("")
self.cookie_box.setObjectName("cookie_box")
self.horizontalLayout_3.addWidget(self.cookie_box)
self.cookie_box.stateChanged.connect(self.order)
bill = 0 #the bill variable
def order(self):
if self.latte_box.isChecked():
bill += 2.85
else:
bill -= 2.85
if self.cookie_box.isChecked():
bill += 1.50
else:
bill -= 1.50
这里的问题是,所有项目都通过方法(订单)运行,并且无论是否选中该框,都会添加价格,如果未选中该框,则会删除价格
如何只有选中或未选中的框才能在方法中运行,而未触及的框保持静止 试试看:
import sys
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.Qt import *
class Window(QWidget):
def __init__(self):
super().__init__()
self.latte_box = QtWidgets.QCheckBox()
self.latte_box.setText("2.85")
self.latte_box.stateChanged.connect(lambda state, cb=self.latte_box: self.order(state, cb))
self.cookie_box = QtWidgets.QCheckBox()
self.cookie_box.setText("1.50")
self.cookie_box.stateChanged.connect(lambda state, cb=self.cookie_box: self.order(state, cb))
self.label = QLabel()
self.layout = QGridLayout(self)
self.layout.addWidget(self.latte_box, 0, 0)
self.layout.addWidget(self.cookie_box, 0, 1)
self.layout.addWidget(self.label, 1, 0)
self.bill = 0
def order(self, state, cb):
if cb is self.latte_box:
if state:
self.bill += 2.85
else:
self.bill -= 2.85
elif cb is self.cookie_box:
if state:
self.bill += 1.50
else:
self.bill -= 1.50
self.label.setText(f'{abs(self.bill):.2f}')
if __name__ == '__main__':
import sys
app = QApplication(sys.argv)
form = Window()
form.show()
sys.exit(app.exec_())
试试看:
import sys
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.Qt import *
class Window(QWidget):
def __init__(self):
super().__init__()
self.latte_box = QtWidgets.QCheckBox()
self.latte_box.setText("2.85")
self.latte_box.stateChanged.connect(lambda state, cb=self.latte_box: self.order(state, cb))
self.cookie_box = QtWidgets.QCheckBox()
self.cookie_box.setText("1.50")
self.cookie_box.stateChanged.connect(lambda state, cb=self.cookie_box: self.order(state, cb))
self.label = QLabel()
self.layout = QGridLayout(self)
self.layout.addWidget(self.latte_box, 0, 0)
self.layout.addWidget(self.cookie_box, 0, 1)
self.layout.addWidget(self.label, 1, 0)
self.bill = 0
def order(self, state, cb):
if cb is self.latte_box:
if state:
self.bill += 2.85
else:
self.bill -= 2.85
elif cb is self.cookie_box:
if state:
self.bill += 1.50
else:
self.bill -= 1.50
self.label.setText(f'{abs(self.bill):.2f}')
if __name__ == '__main__':
import sys
app = QApplication(sys.argv)
form = Window()
form.show()
sys.exit(app.exec_())
我先勾选了两次,然后取消勾选复选框,然后数字开始相互混合others@Pulse你试过我的例子吗?我的例子对你来说不正确吗?或者您是否对您的程序进行了任何更改,您在上面的评论中描述了更改的结果?我尝试了您的示例,在前两次连续“选中/取消选中”复选框时效果良好,然后我发现问题不是您的示例,由于某些原因,编译器在十进制级别上出错,因此解决方案是使用“round(bill,2)”将账单四舍五入逗号后的2位小数。前2次我选中了复选框,然后取消选中复选框,然后数字开始相互混合others@Pulse你试过我的例子吗?我的例子对你来说不正确吗?或者您是否对您的程序进行了任何更改,您在上面的评论中描述了更改的结果?我尝试了您的示例,在前两次连续“选中/取消选中”复选框时效果良好,然后我发现问题不是您的示例,由于某些原因,编译器在十进制级别上出错,因此解决方案是使用“round(bill,2)”将账单四舍五入逗号后的2位小数