PyQt5 Tutorials
About Lesson

In this PyQt5 lesson we want to learn about PyQt5 QRadioButton & QGroupBox, also we are going to learn how we can use toggled signal of RadioButton, in pyqt5 we can use QRadioButton class for creating of radiobutton, also by using GroupBox we can group our  widgets and for this we need to use QGroupBox class in pyqt5.


So first we want to create our pyqt5 radiobutton using coding, and after that we use Qt Designer for creating of the RadioButton.



These are the imports that we need, we have imported QRadioButton,QGroupBox, these are the classes that you can create your radiobutton and groupbox in pyqt5.



These are used for creating title, icon and the geometry of the window, basically the geometry of the window is the x,y position, width and height of the window.



Also in our code we have used QVBoxLayout with QHBoxLayout for the layout management in pyqt5.



You can create groupbox by creating the object of QGroupBox class. also we have set the font for the groupbox.



We want to add our all radiobuttons in the hboxlayout.



Basically we want to create three radiobuttons,  and you can see that i have created icon for my radiobutton, make sure that you have already added some icons in your working directory. also i have have connected radiobutton toggled() signal with the on_selected() method that i have already created, basically this method is used for the user interaction, i want when a user clicks on the radiobutton, my label text should be changed.



And this is the method or slot that we have already connected with the toggled signal of QRadioButton.



This is the code for PyQt5 QRadioButton & QGroupBox




Run the complete code and this is the result.

How to Create RadioButton & GroupBox in PyQt5
How to Create RadioButton & GroupBox in PyQt5



Create RadioButton & GroupBox with Qt Designer

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 a new Dialog without Buttons window.

Now design your application in these steps

  • Add a vertical Layout in the Designer
  • In the vertical layout add a groupbox, change the title and font from the designer
  • After that add an hbox layout
  • In the HBoxLayout you need to add your three radiobutton
  • Also you need to add a label in the VBoxLayout




This is the design, you can add the icons in the designer or you can use coding for adding the icons. 

PyQt5 Qt Designer Example
PyQt5 Qt Designer Example



So now save your UI file and add the file in the scripts folder, because we want to convert our UI file in to Python file. for doing this action we need to use pyuic5 module, it is a module that is located in the Scripts folder of your Python installation, it is important to copy your UI file in the Scripts folder and your terminal write 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.




Run the complete code and this is the result.

Qt Designer RadioButton Example
Qt Designer RadioButton Example