PyQt5: chapter1-understand radio buttons

QRadioButton class

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

The signals that can be transmitted are as follows:

  • toggled (): This signal is emitted when the state of the button changes from selected to unselected, and vice versa
  • clicked (): This signal is emitted when the button is activated (ie, pressed and released) or its shortcut key is pressed
  • stateChanged (): This signal is emitted when the radio button changes its state from selected to unselected or vice versa

Examples

  1. Create a window based on the Dialog without Buttons template
  2. Drag two Label parts and place them in the window
  3. Drag out the three Radio Button widgets and place them in the middle of the window
  4. Set the text property of the first Label component to Choose the flight type and delete the default value of the text property of the second Label component because the text value will be filled in by the program code
  5. Set the text properties of the three Radio Buttons as First Class $ 150, Business Class $ 125, and Economy Class $ 100, and their objectName properties as radioButtonFirstClass, radioButtonBusinessClass, radioButtonEconomyClass
  6. Save as demoRadioButton1.ui file
  7. Use pyuic5 to generate demoRadioButton1.py file
  8. Create callRadioButton1.py file, call demoRadioButton1.py file, the code is as follows

 

 

import sys
from PyQt5.QtWidgets import QDialog,QApplication
from cookbook_200419.demoRadioButton1 import  *


class MyForm(QDialog):
    def __init__(self):
        super().__init__()
        self.ui=Ui_Dialog()
        self.ui.setupUi(self)
        self.ui.radioButtonFirstClass.toggled.connect(self.dispFare)
        self.ui.radioButtonBusinessClass.toggled.connect(self.dispFare)
        self.ui.radioButtonEconomyClass.toggled.connect(self.dispFare)
        self.show()

    def dispFare(self):
        fare=0
        if self.ui.radioButtonFirstClass.isChecked()==True:
            fare=150
        if self.ui.radioButtonBusinessClass.isChecked()==True:
            fare=125
        if self.ui.radioButtonEconomyClass.isChecked()==True:
            fare=100
        self.ui.labelFare.setText("Air Fare is "+str(fare))
if __name__=="__main__":
    app=QApplication(sys.argv)
    w=MyForm()
    w.show()
    sys.exit(app.exec())

 

 

 

Published 120 original articles · won praise 3 · Views 3745

Guess you like

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