【问题标题】:How to run code in only for selected checkboxes in pyqt5?如何仅在pyqt5中为选定的复选框运行代码?
【发布时间】:2020-10-21 21:11:41
【问题描述】:

我正在尝试使用 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("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

latte_box 和 cookie_box 是列表中 2 件商品的复选框,价格分别为 2.85 美元和 1.50 美元。 因此,当用户选中该框时,该项目的价格将被添加到账单中,但如果出现错误,用户只需取消选中该框,该项目的价格将从账单中删除。

这里的问题是,所有的商品都是通过方法(order)运行的,是否勾选则添加价格,不勾选则去掉价格。

如何只有选中或未选中的框通过该方法,而未触摸的框保持静止..?

【问题讨论】:

    标签: checkbox pyqt5


    【解决方案1】:

    试试看:

    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_())
    

    【讨论】:

    • 它在我选中的前 2 次有效,然后取消选中复选框,然后数字开始相互混合
    • @Pulse 你试过我的例子吗?我的示例是否对您不正确?或者您是否对您的程序进行了任何更改,您在上面的评论中描述了结果?
    • 我试过你的例子,它在前 2 个连续的“选中/取消选中”框工作正常,然后我看到问题不是你的例子,由于某种原因,编译器在小数点上出错level 所以解决方案是使用“round(bill, 2)”在逗号后将账单四舍五入到小数点后 2 位
    猜你喜欢
    • 1970-01-01
    • 2012-09-17
    • 2017-12-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-01-18
    • 1970-01-01
    • 2020-09-18
    相关资源
    最近更新 更多