PyQt5: chapter1-理解单选按钮

QRadioButton类

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

其可发射的信号如下:

  • toggled():当按钮的状态从选中变为未选中时,就会发出此信号,反之亦然
  • clicked():当按钮被激活(即按下并松开)或按下其快捷键时,会发出此信号
  • stateChanged():当单选按钮将其状态从选中更改为未选中或反之亦然时,会发出此信号

实例

  1. 创建基于Dialog without Buttons模板的窗口
  2. 拖出两个Label部件一上一下置于窗口
  3. 拖出三个Radio Button部件置于窗口中间
  4. 设定第一个Label部件text属性为Choose the flight type ,删除第二个Label部件的text属性默认值,因为text值将由程序代码填入
  5. 设定三个Radio Button的text属性分别为First Class $150,Business Class $125,and Economy Class $100,其objectName属性分别为radioButtonFirstClass, radioButtonBusinessClass, radioButtonEconomyClass
  6. 保存为demoRadioButton1.ui文件
  7. 使用pyuic5生成demoRadioButton1.py文件
  8. 创建callRadioButton1.py文件,调用demoRadioButton1.py文件,代码如下
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())

 

发布了120 篇原创文章 · 获赞 3 · 访问量 3745

猜你喜欢

转载自blog.csdn.net/weixin_43307431/article/details/105618146
今日推荐