Python PySide6
About Lesson

In this PySide6 lesson we want to learn How to Create QRadioButton in PySide6, so we already know that PySide6 is Python binding for the Qt toolkit that can be used to build GUI applications in Python. One of the widgets that PySide6 provide is the QRadioButton widget. In this lesson, we want to learn what QRadioButtons are, how they work, and how you can use them to build nice GUIs.

 

 

What is a QRadioButton ?

QRadioButton is a button that can be selected by the user to choose from a set of mutually exclusive options. Only one radio button in a set can be selected at any given time. Radio buttons are commonly used in GUIs to provide the user with a way to choose from a set of options.

 

 

How to Create QRadioButtons in PySide6

To create a QRadioButton in PySide6, you can use the QRadioButton class. this is an example of how to create a radio button in python pyside6:

In the above example, we have created QRadioButton. We set the parent of the radio button to be the widget object, which is a QWidget that we’ll use to display the radio button. After that we use the move() method to position the radio button at coordinates (50, 50) inside the widget. And at the end we show the widget and start the event loop with app.exec().

 

 

Run the code and this will be the result

How to Create QRadioButton in PySide6
How to Create QRadioButton in PySide6

 

 

You can create multiple radio buttons and add them to the same parent widget to create a set of mutually exclusive options. This is an example:

In this example we have created three radio buttons and add them to a vertical layout using QVBoxLayout class. The addWidget() method is used to add radio buttons to the layout in order that they should appear. By default, only one radio button in the set can be selected at any given time.

 

 

Run the code and this will be the result

How to Create QRadioButton in PySide6
How to Create QRadioButton in PySide6

 

 

How to Get Selected QRadioButton in PySide6

To retrieve the selected radio button from a set of radio buttons, you can use the isChecked() method of the QRadioButton class. This is an example:

 

 

This is complete example on Python PySide6 QRadioButton

In the above example, we have define new RadioButtons class that inherits from QWidget. In the __init__ method, we have created vertical layout with three radio buttons. after that we have added the radio buttons to the layout and set the layout as the layout of the widget, or we can say the main window layout.

After that we connect the toggled signal of each radio button to a single slot selected_option using the connect() method. The toggled signal is emitted whenever checked state of the radio button changes. In the selected_option method, we check which radio button is selected using the isChecked() method of each radio button, and print a message indicating which option is selected.

 

 

Run the code and this will be the result

QRadioButton in PySide6
QRadioButton in PySide6

Subscribe and Get Free Video Courses & Articles in your Email

 

Codeloop
×