PyQt5: chapter1-show two sets of check boxes

Examples

  1. Create a window based on the Dialog without Buttons template
  2. Drag out the four Label parts, the text properties are Menu, Select your IceCream, Select your drink, the last one is empty
  3. Drag out two Group Boxes whose text attributes are IceCreams and Drinks and their objectName attributes are groupBoxIceCreams and groupBoxDrinks
  4. 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
  5. Drag out three Check Boxes, whose text attributes are Coffee $ 2, Soda $ 3, Tea $ 1, and their objectNames are checkBoxCoffee, checkBoxSoda, checkBoxTea
  6. Set the object name of the bottom Label to labelAmount
  7. Save as demoCheckBox2.ui file
  8. Use pyuic5 to generate demoCheckBox2.py file
  9. 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())

Published 120 original articles · Like 3 · Visitor 3742

Guess you like

Origin blog.csdn.net/weixin_43307431/article/details/105622559