PyQt5 Tutorial – Working with RadioButton

In this PyQt5 Tutorial we are going to learn about Working with RadioButton, QRadioButton is a class in PyQt5 that provides radio button widget for use in graphical user interfaces (GUIs). radio button is GUI component that allows users to select one option from set of mutually exclusive options.

When several radio buttons are grouped together, only one can be selected at a time. when user selects one radio button, all others in the same group are automatically deselected. Radio buttons are commonly used in GUIs for setting options, preferences or other types of binary choices.

In PyQt5, QRadioButton class inherits from the QAbstractButton class which provides the basic functionality for all types of buttons in the Qt framework. QRadioButton class adds additional functionality specific to radio buttons, such as the ability to group radio buttons together and to retrieve the currently selected radio button in a group.

 

 

 

 

Learn Programming with Free Video Courses in Geekscoders.com

 

 

OK now we want to create our layout using Qt Designer , so you need to open your Qt Designer, you can write pyqt5designer in your terminal, after opening create new Dialog without Buttons window.

 

 

Now design your application in these steps

  • Add a QVBoxLayout in the Designer
  • In the QVBoxLayout add a QGroupBox, change the title and font from the designer
  • After that add QHBoxLayout
  • In the QHBoxLayout you need to add your three QRadioButton
  • Also you need to add a QLabel in the QVBoxLayout

 

 

 

This is the design that we want,  you can add the icons in the designer or you can use coding for adding the icons. we will add the icons using coding.

PyQt5 RadioButton Qt Designer
PyQt5 RadioButton Qt Designer

 

 

 

After completing the design you need to save your design, the extension will be .ui file. now it is time to convert our .ui file in to .py file. there are two ways that you can do, first way is loading the ui file, the second way is converting the .ui file to .py file, we want to use the second way. for converting of the ui file to python file we need to use pyuic5 module, this module is located in the Scripts folder of your python installation, you need to copy your ui file in the Scripts folder of your Python installation. after that open the terminal in the Scripts folder, and run this command.

 

 

 

 

 

This is the converted code. so when you convert, the main class will extends from QObject, but you need to change that manually to QDialog, also we have added our on_selected() method at the bottom, because we have connected this method with toggled() signal of QRadioButton. you need to add some icons manually for your radiobuttons, make sure that you have added some icons in your working directory.

 

 

 

This is our method and we have connected this method with the toggled() signal of the radiobuttons, in this method first we get the value from the radiobutton using sender() method. after that we are checking the value for the radiobutton.

 

 

 

 

Run the complete code and this is the result.

PyQt5 Tutorial - Working with RadioButton
PyQt5 Tutorial – Working with RadioButton

 

Subscribe and Get Free Video Courses & Articles in your Email

 

Leave a Comment

Share via
Copy link
Powered by Social Snap
×