Course Content
Python PySide6
About Lesson

In this lesson we want to learn about Python PySide6 QRadioButton, Graphical User Interfaces (GUIs) are a great way to interact with our programs. They provide the user with a visual way of interacting with the program’s functions and data. PySide6 is a 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’ll explore what QRadioButtons are, how they work, and how you can use them to build nice GUIs.

 

 

What is a QRadioButton ?

A 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 i an example of how to create a radio button in python pyside6:

In this example, we creates a QRadioButton with the label “Option 1”. 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) within the widget. Finally, we show the widget and start the event loop with app.exec().

 

 

Run the complete code this will be the result.

Python PySide6 QRadioButton
Python PySide6 QRadioButton

 

 

You can create multiple radio buttons and add them to the same parent widget to create a set of mutually exclusive options. Here’s 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.

Python PySide6 QRadioButton
Python PySide6 QRadioButton

 

 

Retrieving the Selected QRadioButton

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

In this example, we check each radio button in turn using the isChecked() method. The isChecked() method returns True if the radio button is selected, and False otherwise.

 

 

This is complete example on Python PySide6 QRadioButton

In this example, we define new RadioButtons class that inherits from QWidget. In the __init__ method, we create a vertical layout with three radio buttons with labels “Option 1”, “Option 2”, and “Option 3”. 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.

at the end we creates an instance of the RadioButtons class, show it, and start the event loop with app.exec(). This results in a window containing a set of radio buttons that the user can interact with.

By using object-oriented programming, we can encapsulate the logic for creating and handling radio buttons in a single class, this makes our code more modular and easier to maintain.

 

 

Final Thoughts

QRadioButtons are a useful widget for building interactive GUIs in PySide6. They allow the user to choose from a set of mutually exclusive options. You can create a set of radio buttons by adding them to the same parent widget, and retrieve the selected radio button using the isChecked() method. With these tools, you can create powerful and interactive GUIs in Python using PySide6.