I know many of you are just looking for some reference code to copy so I put that here at the top, but if you want to learn more, a detailed explanation can be found in the video above and in the text below.
import sys
from PyQt5.QtWidgets import (QApplication, QMainWindow, QVBoxLayout, QWidget,
QButtonGroup, QRadioButton, QLabel)
class MainWindow(QMainWindow):
def __init__(self):
super().__init__()
self.setWindowTitle("My QRadioButton Window")
main_layout = QVBoxLayout()
# 1. Creating the button group and radio buttons
self.button_group = QButtonGroup()
self.radio_button1 = QRadioButton("Apple")
self.radio_button2 = QRadioButton("Peach")
self.radio_button3 = QRadioButton("Grapes")
self.button_group.addButton(self.radio_button1)
self.button_group.addButton(self.radio_button2)
self.button_group.addButton(self.radio_button3)
main_layout.addWidget(self.radio_button1)
main_layout.addWidget(self.radio_button2)
main_layout.addWidget(self.radio_button3)
# 2. Setting checked
self.radio_button1.setChecked(True)
# 3. Getting checked button and text
self.label = QLabel("Selected radio button: {}".format(
self.button_group.checkedButton().text()))
main_layout.addWidget(self.label)
# 4. Button group buttonClicked signal
self.button_group.buttonClicked.connect(self.on_button_click)
widget = QWidget()
widget.setLayout(main_layout)
self.setCentralWidget(widget)
def on_button_click(self, button):
self.label.setText("Selected radio button: {}".format(button.text()))
def main():
app = QApplication(sys.argv)
window = MainWindow()
window.show()
app.exec()
if __name__ == '__main__':
main()
Boilerplate Code
For us to work with QRadioButton
s, we will need to setup a window to quickly do that. The code for which can be found below:
import sys
from PyQt5.QtWidgets import (QApplication, QMainWindow, QVBoxLayout,
QWidget)
class MainWindow(QMainWindow):
def __init__(self):
super().__init__()
self.setWindowTitle("My QRadioButton Window")
main_layout = QVBoxLayout()
widget = QWidget()
widget.setLayout(main_layout)
self.setCentralWidget(widget)
def main():
app = QApplication(sys.argv)
window = MainWindow()
window.show()
app.exec()
if __name__ == '__main__':
main()
Starting off at the top, we have some imports for the things we will need.
Lines 5-8, subclass QMainWindow
in order to allow us to create our own custom main window to add content to. Then it initializes the class by running the super class’ init()
method, and setting the title for the window to "My QRadioButton Window"
.
Line 9 created a QVBoxLayout. Lines 11-13 create a widget, adds the layout to that widget and sets it as the central widget for this application.
If you haven’t worked with layouts before, I have a tutorial covering them, but for now all you really need to know is that this line, and the lines below it make it so you can add widgets (like labels and push buttons) to your interface so that they are stacked vertically and stretch nicely when the window is resized.
Lines 16-20 define the main()
function which creates an application initialized with the given command line arguments (sys.argv
), creates a main window from our custom class, shows the window, and executes the application.
Lastly, lines 23 and 24 runs the main()
function if this is the main file being run.
If you save the file and run it, it should look like this:
Now let’s check some boxes!
Creating QRadioButtons and a QButtonGroup
Starting off, we need to update line 3 to import QCheckBox
:
from PyQt5.QtWidgets import (QApplication, QMainWindow, QVBoxLayout, QWidget,
QButtonGroup, QRadioButton)
On line 11 we are going to create a QButtonGroup
in the variable self.button_group
. Button groups connect multiple buttons together. They are commonly used with QRadioButton
s. By default, they make it so the buttons in the button group are exclusive, meaning you can only have on selected at a time.
Then, on line 12-14, we create three radio buttons, initializing them with a string that will be the text next to the radio button.
self.button_group = QButtonGroup()
self.radio_button1 = QRadioButton("Apple")
self.radio_button2 = QRadioButton("Peach")
self.radio_button3 = QRadioButton("Grapes")
Then below that, we add the buttons to the button group with the addButton()
method, passing in the button we want to add.
After that, to put the radio buttons in the window, we use addWidget()
method on the main_layout
, passing it each of the radio buttons we want to add.
self.button_group.addButton(self.radio_button1)
self.button_group.addButton(self.radio_button2)
self.button_group.addButton(self.radio_button3)
main_layout.addWidget(self.radio_button1)
main_layout.addWidget(self.radio_button2)
main_layout.addWidget(self.radio_button3)
Click here for the full code example…
import sys
from PyQt5.QtWidgets import (QApplication, QMainWindow, QVBoxLayout, QWidget,
QButtonGroup, QRadioButton)
class MainWindow(QMainWindow):
def __init__(self):
super().__init__()
self.setWindowTitle("My QRadioButton Window")
main_layout = QVBoxLayout()
self.button_group = QButtonGroup()
self.radio_button1 = QRadioButton("Apple")
self.radio_button2 = QRadioButton("Peach")
self.radio_button3 = QRadioButton("Grapes")
self.button_group.addButton(self.radio_button1)
self.button_group.addButton(self.radio_button2)
self.button_group.addButton(self.radio_button3)
main_layout.addWidget(self.radio_button1)
main_layout.addWidget(self.radio_button2)
main_layout.addWidget(self.radio_button3)
widget = QWidget()
widget.setLayout(main_layout)
self.setCentralWidget(widget)
def main():
app = QApplication(sys.argv)
window = MainWindow()
window.show()
app.exec()
if __name__ == '__main__':
main()
And if we run that code we should get:
Setting the State
It can be useful to set which radio button is selected.
It is pretty straightforward to do this. All we need to do is run the setChecked()
method on the radio button, passing a boolean on whether want it to be checked or not. In our example we are passing it True
. On line 24, we add that to the code:
main_layout.addWidget(self.radio_button3)
self.radio_button1.setChecked(True)
Click here for the full code example…
import sys
from PyQt5.QtWidgets import (QApplication, QMainWindow, QVBoxLayout, QWidget,
QButtonGroup, QRadioButton)
class MainWindow(QMainWindow):
def __init__(self):
super().__init__()
self.setWindowTitle("My QRadioButton Window")
main_layout = QVBoxLayout()
self.button_group = QButtonGroup()
self.radio_button1 = QRadioButton("Apple")
self.radio_button2 = QRadioButton("Peach")
self.radio_button3 = QRadioButton("Grapes")
self.button_group.addButton(self.radio_button1)
self.button_group.addButton(self.radio_button2)
self.button_group.addButton(self.radio_button3)
main_layout.addWidget(self.radio_button1)
main_layout.addWidget(self.radio_button2)
main_layout.addWidget(self.radio_button3)
self.radio_button1.setChecked(True)
widget = QWidget()
widget.setLayout(main_layout)
self.setCentralWidget(widget)
def main():
app = QApplication(sys.argv)
window = MainWindow()
window.show()
app.exec()
if __name__ == '__main__':
main()
And if we run that code we should get:
Getting the State
It can be useful to get what radio button is selected.
For our example, we are going to add a label to the window that will show which radio button is selected.
Starting off, we will need to add an import to QLabel
at the top.
from PyQt5.QtWidgets import (QApplication, QMainWindow, QVBoxLayout, QWidget,
QButtonGroup, QRadioButton, QLabel)
Then we will need to add the label, and set the text. We create a label by running QLabel()
, and save it to the variable self.label. We can initialize the text by passing in a string. We want that string to be the selected radio button.
We can get the selected radio button from the button group by running checkedButton() on the button group. Once we have the selected button, we can get its text by running the text() method on it. We can then combine that with the text "Selected radio button:"
by using the format()
method on the string.
self.radio_button1.setChecked(True)
self.label = QLabel("Selected radio button: {}".format(
self.button_group.checkedButton().text()))
main_layout.addWidget(self.label)
Click here for the full code example…
import sys
from PyQt5.QtWidgets import (QApplication, QMainWindow, QVBoxLayout, QWidget,
QButtonGroup, QRadioButton, QLabel)
class MainWindow(QMainWindow):
def __init__(self):
super().__init__()
self.setWindowTitle("My QRadioButton Window")
main_layout = QVBoxLayout()
self.button_group = QButtonGroup()
self.radio_button1 = QRadioButton("Apple")
self.radio_button2 = QRadioButton("Peach")
self.radio_button3 = QRadioButton("Grapes")
self.button_group.addButton(self.radio_button1)
self.button_group.addButton(self.radio_button2)
self.button_group.addButton(self.radio_button3)
main_layout.addWidget(self.radio_button1)
main_layout.addWidget(self.radio_button2)
main_layout.addWidget(self.radio_button3)
self.radio_button1.setChecked(True)
self.label = QLabel("Selected radio button: {}".format(
self.button_group.checkedButton().text()))
main_layout.addWidget(self.label)
widget = QWidget()
widget.setLayout(main_layout)
self.setCentralWidget(widget)
def main():
app = QApplication(sys.argv)
window = MainWindow()
window.show()
app.exec()
if __name__ == '__main__':
main()
And if we run that code, we should see:
Callback Function
Lastly, it can also be useful to run some code whenever a user clicks on a radio button. In our example we will update the label when a user clicks on a radio button.
Below where we added the label to the main layout, we will want to connect the button group’s buttonClicked
signal to a callback function that we will define later. We will call this function self.on_button_click
. Be careful not to add ()
after self.on_button_click
. We want to pass the function, not run it.
main_layout.addWidget(self.label)
self.button_group.buttonClicked.connect(self.on_button_click)
Below the init()
method, we define our on_button_click()
function. The signal will provide the function with the button clicked, so we add that as a parameter. Then all we need to do is update the text of the label using the setText()
method, passing the text we want to set. In our case, we format similar text to how we did it before, giving the button’s text using the button’s text()
method.
self.setCentralWidget(widget)
def on_button_click(self, button):
self.label.setText("Selected radio button: {}".format(button.text()))
Click here for the full code example…
import sys
from PyQt5.QtWidgets import (QApplication, QMainWindow, QVBoxLayout, QWidget,
QButtonGroup, QRadioButton, QLabel)
class MainWindow(QMainWindow):
def __init__(self):
super().__init__()
self.setWindowTitle("My QRadioButton Window")
main_layout = QVBoxLayout()
self.button_group = QButtonGroup()
self.radio_button1 = QRadioButton("Apple")
self.radio_button2 = QRadioButton("Peach")
self.radio_button3 = QRadioButton("Grapes")
self.button_group.addButton(self.radio_button1)
self.button_group.addButton(self.radio_button2)
self.button_group.addButton(self.radio_button3)
main_layout.addWidget(self.radio_button1)
main_layout.addWidget(self.radio_button2)
main_layout.addWidget(self.radio_button3)
self.radio_button1.setChecked(True)
self.label = QLabel("Selected radio button: {}".format(
self.button_group.checkedButton().text()))
main_layout.addWidget(self.label)
self.button_group.buttonClicked.connect(self.on_button_click)
widget = QWidget()
widget.setLayout(main_layout)
self.setCentralWidget(widget)
def on_button_click(self, button):
self.label.setText("Selected radio button: {}".format(button.text()))
def main():
app = QApplication(sys.argv)
window = MainWindow()
window.show()
app.exec()
if __name__ == '__main__':
main()
If we run that code, and click on the Peach radio button, we will see:
And it works similarly for any of the radio buttons you click.
And those are the essentials of QRadioButton
s!