How to make button like radiobuttons in PyQt? How to make button like radiobuttons in PyQt? tkinter tkinter

How to make button like radiobuttons in PyQt?


Why don't you use a QButtonGroup? It's exclusive by default and helps you keeping track and reacting on the events when you click on an option.

Code example:

from PyQt5.QtWidgets import *app = QApplication([])w = QWidget()w.setWindowTitle('pyqt')l = QVBoxLayout(w)l.setContentsMargins(0, 0, 0, 0)l.addWidget(QLabel('Choose your favorite programming language:'))titles = ['Python', 'Perl', 'Java', 'C++', 'C']buttons = [QPushButton(title) for title in titles]button_group = QButtonGroup()for button in buttons:    l.addWidget(button)    button_group.addButton(button)    button.setCheckable(True)w.show()app.exec()

which looks like your example except for differences in style (use Qt stylesheets for that).

enter image description here