Reputation: 5408
I created a QButtonGroup
(there might be unused artefacts in the code. Please ignore them). I would like to retrieve the Id of all checked radio buttons. However, the QButtonGroup.checkedId()
does return only one button at a time.
Is it possible to retrieve a list of all checked RadioButtons ?
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())
Upvotes: 1
Views: 2329
Reputation: 5893
You could just retrieve them with a list comprehension:
def test(self):
print([i for i, button in enumerate(self.radio_buttons.button_group.buttons()) if button.isChecked()])
Upvotes: 3