qbuttongroup如何都不选中_夸脱QButtonGroup.checkedId()只返回一个选中的按钮

本文档展示了如何在PyQt5中创建一个QButtonGroup,并且遇到一个问题:QButtonGroup.checkedId()只能返回一个选中按钮的ID。作者寻求一种方法来获取所有选中状态的单选按钮的ID列表。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

我创建了一个QButtonGroup(代码中可能有未使用的人工制品)。请忽略它们)。我想检索所有选中单选按钮的Id。但是,QButtonGroup.checkedId()一次只返回一个按钮。在

是否可以检索所有选中单选按钮的列表?在import sys

from PyQt5.QtWidgets import QMainWindow, QApplication, QPushButton, QWidget, QAction, QTabWidget, QVBoxLayout,\

QHBoxLayout, QGridLayout, QSplitter, QFrame, QTextEdit, QFileDialog, QGroupBox,\

QButtonGroup, QLabel, QRadioButton

class MyRadioButtonGroup(QWidget):

"""

creates radio buttons from a list

"""

def __init__(self, label, instructions, name_list):

super().__init__()

self.title_label = QLabel(label)

self.name_list = name_list

# create the widget

self.layout = QVBoxLayout()

self.group_box = QGroupBox(instructions)

self.button_group = QButtonGroup()

self.button_group.setExclusive(False)

# create the buttons

self.button_list = [QRadioButton(item) for item in self.name_list]

self.button_layout = QVBoxLayout()

for i, button in enumerate(self.button_list):

self.button_layout.addWidget(button)

self.button_group.addButton(button)

self.button_group.setId(button, i)

self.group_box.setLayout(self.button_layout)

# create the main layout

self.main_layout = QVBoxLayout()

self.main_layout.addWidget(self.title_label)

self.main_layout.addWidget(self.group_box)

self.setLayout(self.main_layout)

# method for getting the state of the buttons

def get_states(self):

states = self.button_group.checkedId()

return states

class MainApp(QMainWindow):

""" This creates the main Window and configures its look

"""

def __init__(self):

'''

Initializes the class with standard parameters

'''

super().__init__()

self.title = 'Data Analysis App'

# start at top left corner of the screen

self.left = 0

self.top = 0

# set the window height and width

self.width = 1280

self.height = 780

# set the styles of fonts etc.

self.setStyleSheet('font-size: 16pt')

# call the makeUI method

self.makeUI()

def makeUI(self):

# set window geometry

self.setWindowTitle(self.title)

self.setGeometry(self.left, self.top, self.width, self.height)

# generate the tab layout

self.table_widget = MyTableWidget()

self.setCentralWidget(self.table_widget)

# apparently we have to call the show method in order to show it

self.show()

class MyTableWidget(QWidget):

"""

Initializes the tab layout

"""

def __init__(self):

super().__init__()

self.layout = QVBoxLayout(self)

# initialize the tab screens

self.tabs = QTabWidget()

# add the tab screens to the tab widget

self.tabs.resize(600, 600)

# make the layout of the tabs

self.make_tab1()

# initialize the tabs

self.layout.addWidget(self.tabs)

self.setLayout(self.layout)

def make_tab1(self):

"""Style of tab 1 is created here"""

'''Defines master layout of tab1'''

tab1 = QWidget()

tab1.layout = QHBoxLayout()

'''Button Section'''

# converts the data into excel and pandas dataframe

btn_test = QPushButton('Test')

btn_test.clicked.connect(self.test)

name_list = ['A', 'B', 'C']

self.radio_buttons = MyRadioButtonGroup('TEST', 'Check more than one', name_list)

'''Page layout section '''

tab1.layout.addWidget(btn_test)

tab1.layout.addWidget(self.radio_buttons)

tab1.setLayout(tab1.layout)

self.tabs.addTab(tab1, 'Test Radio Buttons')

''' Methods section

'''

def test(self):

print(self.radio_buttons.get_states())

if __name__ == '__main__':

""" This code block is always the same and makes sure that e.g. STRG+C kills the window etc.

"""

app = QApplication(sys.argv)

ex = MainApp()

sys.exit(app.exec())

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值