mardi 20 octobre 2020

checkboxes in PyQt5

Im trying to make a menu GUI using pyqt5, the menu includes drinks and stuffs etc.. in front of each menu item there is a checkbox, when it is checked its price will be added to the bill.

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 and cookie_box are the checkboxes of 2 items on the list with the prices of $2.85 and $1.50, so when the user check the box, the price of the item will be added to the bill, but in case of an error the user will just uncheck the box and the price of the item will be removed from the bill.

the problem here is that all the items run through the method (order), and whether the box is checked the price is added, and if its not checked the price is removed.

how can only the boxes that are checked or unchecked run through the method, and the untouched ones remain still.. ?




Aucun commentaire:

Enregistrer un commentaire