How can I check if a generated radio button is checked in PyQt5?

  Kiến thức lập trình

I’m creating a class roster with PyQt5.

I generated several radio buttons along with each student entry. I want to be able to check the radio button and click another button to delete the entry. I am having trouble identifying which radio button is clicked because they have no unique name attached. Any suggestions? TIA.

for student in all_students:
    name_info= f'{student[0]}, {student[1]} ({str(student[2])})'
    radioButton = QtWidgets.QRadioButton(self)
    radioButton.setText(name_info)
    radioButton.setFont(QtGui.QFont('Courier', 15))
    form_layout.addRow(radioButton)

form_layout.addRow(QtWidgets.QPushButton('Delete Student', clicked = lambda: delete_student(self)))

def delete_student(self):
    if radioButton.isChecked():
        print(radioButton.text())

FYI – if this code worked properly, it would just print radio button text (which is all I need right now), but I am getting nothing.

I also tried putting each generated radio button in a list and then iterate through them to check if they were checked, but that threw an error too.

Here are what the radio buttons look like in the app if that helps:

3

LEAVE A COMMENT