Examples
- Create a window based on the Dialog without Buttons template
- Drag out the four Label parts, the text properties are Menu, Select your IceCream, Select your drink, the last one is empty
- Drag out two Group Boxes whose text attributes are IceCreams and Drinks and their objectName attributes are groupBoxIceCreams and groupBoxDrinks
- Drag out the four Check Boxes on the Group Box, whose text attributes are Mint Choclate Chips $ 4, Cookie Dough $ 2, Choclate Almond $ 3, Rocky Road $ 5, and their objectNames are checkBoxChoclateChips, checkBoxCookieDough, checkBoxChoclateAlmond, checkBoxRockyRoad
- Drag out three Check Boxes, whose text attributes are Coffee $ 2, Soda $ 3, Tea $ 1, and their objectNames are checkBoxCoffee, checkBoxSoda, checkBoxTea
- Set the object name of the bottom Label to labelAmount
- Save as demoCheckBox2.ui file
- Use pyuic5 to generate demoCheckBox2.py file
- Create callCheckBox2.py file, call demoCheckBox2.py file, the code is as follows
import sys
from PyQt5.QtWidgets import QDialog
from PyQt5.QtWidgets import QApplication,QWidget,QPushButton
from cookbook_200419.demoCheckBox2 import *
class MyForm(QDialog):
def __init__(self):
super().__init__()
self.ui=Ui_Dialog()
self.ui.setupUi(self)
self.ui.checkBoxChoclateAlmond.stateChanged.connect(self.dispAmount)
self.ui.checkBoxChoclateChips.stateChanged.connect(self.dispAmount)
self.ui.checkBoxCookieDough.stateChanged.connect(self.dispAmount)
self.ui.checkBoxRockyRoad.stateChanged.connect(self.dispAmount)
self.ui.checkBoxCoffee.stateChanged.connect(self.dispAmount)
self.ui.checkBoxSoda.stateChanged.connect(self.dispAmount)
self.ui.checkBoxTea.stateChanged.connect(self.dispAmount)
self.show()
def dispAmount(self):
amount=0
if self.ui.checkBoxChoclateAlmond.isChecked()==True:
amount=amount+3
if self.ui.checkBoxChoclateChips.isChecked()==True:
amount=amount+4
if self.ui.checkBoxCookieDough.isChecked()==True:
amount=amount+2
if self.ui.checkBoxRockyRoad.isChecked()==True:
amount=amount+5
if self.ui.checkBoxCoffee.isChecked()==True:
amount=amount+2
if self.ui.checkBoxSoda.isChecked()==True:
amount=amount+3
if self.ui.checkBoxTea.isChecked()==True:
amount=amount+1
self.ui.labelAmount.setText("Total amount is $"+str(amount))
if __name__=="__main__":
app=QApplication(sys.argv)
w=MyForm()
w.show()
sys.exit(app.exec())