鱼C论坛

 找回密码
 立即注册
查看: 1410|回复: 1

[已解决]如何获取Pyside6中单选项的当前内容?

[复制链接]
发表于 2023-12-30 10:16:41 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
已经存在了两个QRadioButton控件,变量名分别是RadioButton_1和RadioButton_2,RadioButton_1对应的单选内容是 ‘男性’ ,RadioButton_2对应的单选内容是 ‘女性’。
并且这两个QRadioButton控件组和成一个QButtonGroup按钮组控件,这个按钮组的变量名是ButtonGroup_1.
比如我设置了RadioButton_2的 ‘女性’为当前默认选中状态,
请问如何通过代码获取当前选中状态的QRadioButton对应的内容?
最佳答案
2023-12-30 10:16:53
在PySide6中,你可以通过QButtonGroup的checkedId()方法获取当前选中的QRadioButton的id,然后根据id来获取对应的内容。

以下是一个示例代码:
from PySide6.QtWidgets import QApplication, QMainWindow, QRadioButton, QButtonGroupclass MainWindow(QMainWindow):    def __init__(self):        super().__init__()        self.button_group = QButtonGroup()        self.radio_button_1 = QRadioButton('男性')        self.radio_button_2 = QRadioButton('女性')        self.button_group.addButton(self.radio_button_1)        self.button_group.addButton(self.radio_button_2)        self.button_group.setId(self.radio_button_1, 0)   # 将id与QRadioButton关联        self.button_group.setId(self.radio_button_2, 1)        self.button_group.buttonClicked.connect(self.get_selected_content)        self.setCentralWidget(self.radio_button_1)    def get_selected_content(self):        selected_button_id = self.button_group.checkedId()        if selected_button_id == 0:            print('当前选中的内容是:男性')        elif selected_button_id == 1:            print('当前选中的内容是:女性')if __name__ == '__main__':    app = QApplication([])    window = MainWindow()    window.show()    app.exec()
在这个例子中,我们创建了一个QMainWindow窗口,其中包含了两个QRadioButton控件,通过QButtonGroup将它们组合成一个按钮组。在按钮组的buttonClicked信号触发时,我们调用get_selected_content方法来获取当前选中的QRadioButton的内容。

注意,我们使用setId方法为每个QRadioButton关联了一个id。这个id会在调用checkedId方法时返回,以表示当前选中的QRadioButton。然后,我们根据id来判断当前选中的内容,并打印出来。

在运行这段代码后,如果默认选中了'女性',则控制台会输出:
当前选中的内容是:女性

以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2023-12-30 10:16:53 | 显示全部楼层    本楼为最佳答案   
在PySide6中,你可以通过QButtonGroup的checkedId()方法获取当前选中的QRadioButton的id,然后根据id来获取对应的内容。

以下是一个示例代码:
from PySide6.QtWidgets import QApplication, QMainWindow, QRadioButton, QButtonGroupclass MainWindow(QMainWindow):    def __init__(self):        super().__init__()        self.button_group = QButtonGroup()        self.radio_button_1 = QRadioButton('男性')        self.radio_button_2 = QRadioButton('女性')        self.button_group.addButton(self.radio_button_1)        self.button_group.addButton(self.radio_button_2)        self.button_group.setId(self.radio_button_1, 0)   # 将id与QRadioButton关联        self.button_group.setId(self.radio_button_2, 1)        self.button_group.buttonClicked.connect(self.get_selected_content)        self.setCentralWidget(self.radio_button_1)    def get_selected_content(self):        selected_button_id = self.button_group.checkedId()        if selected_button_id == 0:            print('当前选中的内容是:男性')        elif selected_button_id == 1:            print('当前选中的内容是:女性')if __name__ == '__main__':    app = QApplication([])    window = MainWindow()    window.show()    app.exec()
在这个例子中,我们创建了一个QMainWindow窗口,其中包含了两个QRadioButton控件,通过QButtonGroup将它们组合成一个按钮组。在按钮组的buttonClicked信号触发时,我们调用get_selected_content方法来获取当前选中的QRadioButton的内容。

注意,我们使用setId方法为每个QRadioButton关联了一个id。这个id会在调用checkedId方法时返回,以表示当前选中的QRadioButton。然后,我们根据id来判断当前选中的内容,并打印出来。

在运行这段代码后,如果默认选中了'女性',则控制台会输出:
当前选中的内容是:女性

以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-9-21 22:07

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表