PyQt5: chapter1-分组单选按钮

实例

 

  1. 创建基于Dialog without Buttons模板的窗口
  2. 拖出三个Label部件,其中Label部件分别置于顶部,中部和下部位置
  3. 在中间空白区域拖入两个Vertical Layout面板,在第一个面板上拖入四个Radio Button部件,第二个面板拖入三个Radio Button部件
  4. 第一个Label的text属性为Choose your Shirt Size ,第二个为 Choose your payment method ,第三个置空
  5. 第一个Vertical Layout面板objectName属性设为VBoxLayout,第二个设为QVBoxLayout
  6. 第一个面板上四个Radio Button的text属性分别为M,L,XL,XXL,其objectName分别为radioButtonMedium,radioButtonLarge, radioButtonXL, radioButtonXXL
  7. 第二个面板上四个Radio Button的text属性分别为 Debit/Credit Card,NetBanking, Cash On Delivery,其objectName分别为radioButtonDebitCard,radioButtonNetBanking, radioButtonCashOnDelivery
  8. 第三个Label的objectName属性为labelSelected
  9. 保存为demoRadioButton2.ui文件
  10. 使用pyuic5生成demoRadioButton2.py文件
  11. 创建callRadioButton2.py文件,调用demoRadioButton2.py文件,代码如下

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

class MyForm(QDialog):
    def __init__(self):
        super().__init__()
        self.ui=Ui_Dialog()
        self.ui.setupUi(self)
        self.ui.radioButtonMedium.toggled.connect(self.dispSelected)
        self.ui.radioButtonLarge.toggled.connect(self.dispSelected)
        self.ui.radioButtonXL.toggled.connect(self.dispSelected)
        self.ui.radioButtonXXL.toggled.connect(self.dispSelected)
        self.ui.radioButtonDebitCard.toggled.connect(self.dispSelected)
        self.ui.radioButtonNetBanking.toggled.connect(self.dispSelected)
        self.ui.radioButtonCashOnDelivery.toggled.connect(self.dispSelected)
        self.show()
    def dispSelected(self):
        selected1=""
        selected2=""
        if self.ui.radioButtonMedium.isChecked()==True:
            selected1="Medium"
        if self.ui.radioButtonLarge.isChecked()==True:
            selected1="Large"
        if self.ui.radioButtonXL.isChecked()==True:
            selected1="Extra Large"
        if self.ui.radioButtonXXL.isChecked()==True:
            selected1="Extra Extra Large"
        if self.ui.radioButtonDebitCard.isChecked()==True:
            selected2="Debit/Credit Card"
        if self.ui.radioButtonNetBanking.isChecked()==True:
            selected2="NetBanking"
        if self.ui.radioButtonCashOnDelivery.isChecked()==True:
            selected2="Cash On Delivery"
        self.ui.labelSelected.setText("Chosen shirt size is "+str(selected1)+" and payment method as "+str(selected2))
if __name__=="__main__":
    app=QApplication(sys.argv)
    w=MyForm()
    w.show()
    sys.exit(app.exec())

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值