PyQt5: chapter1-show options in the form of check boxes

QCheckBox class

  • isChecked()
  • setTristate()
  • setIcon ()
  • setText()
  • setChecked()

The signals sent by QCheckBox are as follows:

  • clicked()
  • stateChanged()

Examples

  1. Create a window based on the Dialog without Buttons template
  2. Drag out three Label parts and three Check Box parts, as shown
  3. Set the text properties of the first two Label components to Regular Pizza $ 10 and Select your extra toppings
  4. Set the objectName of the third Label component to labelAmount
  5. Set the text of the three Check Box parts to Extra Cheese $ 1, Extra Olives $ 1, and Extra Sausages $ 2
  6. Set the objectName of the three Check Box parts to checkBoxCheese, checkBoxOlives, checkBoxSausages
  7. Save as demoCheckBox1.ui file
  8. Use pyuic5 to generate demoCheckBox1.py file
  9. Create callCheckBox1.py file, call demoCheckBox1.py file, the code is as follows

import sys
from PyQt5.QtWidgets import QDialog
from PyQt5.QtWidgets import QApplication,QWidget,QPushButton
from cookbook_200419.demoCheckBox1 import *

class MyForm(QDialog):
    def __init__(self):
        super().__init__()
        self.ui=Ui_Dialog()
        self.ui.setupUi(self)
        self.ui.checkBoxCheese.stateChanged.connect(self.dispAmount)
        self.ui.checkBoxOlives.stateChanged.connect(self.dispAmount)
        self.ui.checkBoxSausages.stateChanged.connect(self.dispAmount)
        self.show()
    def dispAmount(self):
        amount=10
        if self.ui.checkBoxCheese.isChecked()==True:
            amount=amount+1
        if self.ui.checkBoxOlives.isChecked()==True:
            amount=amount+1
        if self.ui.checkBoxSausages.isChecked()==True:
            amount=amount+2
        self.ui.labelAmount.setText("Total amount for pizza is "+str(amount))

if __name__=="__main__":
    app=QApplication(sys.argv)
    w=MyForm()
    w.show()
    sys.exit(app.exec())

 

 

Published 120 original articles · won praise 3 · Views 3743

Guess you like

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