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.
1 2 3 4 5 |
from PyQt5.QtWidgets import QApplication,QDialog, QGroupBox,\ QHBoxLayout,QLabel ,QVBoxLayout, QRadioButton import sys from PyQt5.QtGui import QIcon, QFont from PyQt5.QtCore import QSize |
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.
1 2 3 |
self.setGeometry(200,200, 400,300) self.setWindowTitle("PyQt5 QRadioButton") self.setWindowIcon(QIcon('python.png')) |
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.
1 2 |
self.groupbox = QGroupBox("What is your favorite sport ?") self.groupbox.setFont(QFont("Sanserif", 15)) |
We want to add our all radiobuttons in the hboxlayout.
1 |
hbox = QHBoxLayout() |
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.
1 2 3 4 5 6 7 |
self.rad1 = QRadioButton("Football") self.rad1.setChecked(True) self.rad1.setIcon(QIcon('football.png')) self.rad1.setIconSize(QSize(40,40)) self.rad1.setFont(QFont("Sanserif", 14)) self.rad1.toggled.connect(self.on_selected) hbox.addWidget(self.rad1) |
And this is the method or slot that we have already connected with the toggled signal of QRadioButton.
1 2 3 4 5 |
def on_selected(self): radio_button = self.sender() if radio_button.isChecked(): self.label.setText("You have selected : " + radio_button.text()) |
This is the code for PyQt5 QRadioButton & QGroupBox
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 |
from PyQt5.QtWidgets import QApplication,QDialog, QGroupBox,\ QHBoxLayout,QLabel ,QVBoxLayout, QRadioButton import sys from PyQt5.QtGui import QIcon, QFont from PyQt5.QtCore import QSize class Window(QDialog): def __init__(self): super().__init__() #winow requirement self.setGeometry(200,200, 400,300) self.setWindowTitle("PyQt5 QRadioButton") self.setWindowIcon(QIcon('python.png')) #our method call self.create_radiobutton() #we need to create vertical layout vbox = QVBoxLayout() vbox.addWidget(self.groupbox) #this is our label self.label = QLabel("") self.label.setFont(QFont("Sanserif", 14)) #add your widgets in the vbox layout vbox.addWidget(self.label) #set your main window layout self.setLayout(vbox) def create_radiobutton(self): #this is our groupbox self.groupbox = QGroupBox("What is your favorite sport ?") self.groupbox.setFont(QFont("Sanserif", 15)) #this is hbox layout hbox = QHBoxLayout() #these are the radiobuttons self.rad1 = QRadioButton("Football") self.rad1.setChecked(True) self.rad1.setIcon(QIcon('football.png')) self.rad1.setIconSize(QSize(40,40)) self.rad1.setFont(QFont("Sanserif", 14)) self.rad1.toggled.connect(self.on_selected) hbox.addWidget(self.rad1) self.rad2 = QRadioButton("Cricket") self.rad2.setIcon(QIcon('cricket.png')) self.rad2.setIconSize(QSize(40, 40)) self.rad2.setFont(QFont("Sanserif", 14)) self.rad2.toggled.connect(self.on_selected) hbox.addWidget(self.rad2) self.rad3 = QRadioButton("Tennis") self.rad3.setIcon(QIcon('tennis.png')) self.rad3.setIconSize(QSize(40, 40)) self.rad3.setFont(QFont("Sanserif", 14)) self.rad3.toggled.connect(self.on_selected) hbox.addWidget(self.rad3) self.groupbox.setLayout(hbox) #method or slot for the toggled signal def on_selected(self): radio_button = self.sender() if radio_button.isChecked(): self.label.setText("You have selected : " + radio_button.text()) app = QApplication(sys.argv) window = Window() window.show() sys.exit(app.exec_()) |
Run the complete code and this is the result.
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.
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.
1 |
pyuic5 radiobutton.ui -o myradiobuttonexample.py -x |
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 |
# -*- coding: utf-8 -*- # Form implementation generated from reading ui file 'radiobutton.ui' # # Created by: PyQt5 UI code generator 5.15.1 # # WARNING: Any manual changes made to this file will be lost when pyuic5 is # run again. Do not edit this file unless you know what you are doing. from PyQt5 import QtCore, QtGui, QtWidgets from PyQt5.QtWidgets import QDialog from PyQt5.QtGui import QIcon from PyQt5.QtCore import QSize class Ui_Dialog(QDialog): def setupUi(self, Dialog): Dialog.setObjectName("Dialog") Dialog.resize(506, 212) self.verticalLayout_2 = QtWidgets.QVBoxLayout(Dialog) self.verticalLayout_2.setObjectName("verticalLayout_2") self.verticalLayout = QtWidgets.QVBoxLayout() self.verticalLayout.setObjectName("verticalLayout") self.groupBox = QtWidgets.QGroupBox(Dialog) font = QtGui.QFont() font.setPointSize(14) font.setBold(True) font.setWeight(75) self.groupBox.setFont(font) self.groupBox.setObjectName("groupBox") self.horizontalLayoutWidget = QtWidgets.QWidget(self.groupBox) self.horizontalLayoutWidget.setGeometry(QtCore.QRect(10, 20, 401, 81)) self.horizontalLayoutWidget.setObjectName("horizontalLayoutWidget") self.horizontalLayout = QtWidgets.QHBoxLayout(self.horizontalLayoutWidget) self.horizontalLayout.setContentsMargins(0, 0, 0, 0) self.horizontalLayout.setObjectName("horizontalLayout") self.radioButton_3 = QtWidgets.QRadioButton(self.horizontalLayoutWidget) self.radioButton_3.setObjectName("radioButton_3") self.radioButton_3.setIcon(QIcon('football.png')) self.radioButton_3.setIconSize(QSize(40, 40)) self.radioButton_3.toggled.connect(self.on_selected) self.horizontalLayout.addWidget(self.radioButton_3) self.radioButton_2 = QtWidgets.QRadioButton(self.horizontalLayoutWidget) self.radioButton_2.setObjectName("radioButton_2") self.horizontalLayout.addWidget(self.radioButton_2) self.radioButton_2.setIcon(QIcon('cricket.png')) self.radioButton_2.setIconSize(QSize(40, 40)) self.radioButton_2.toggled.connect(self.on_selected) self.radioButton = QtWidgets.QRadioButton(self.horizontalLayoutWidget) self.radioButton.setObjectName("radioButton") self.radioButton.setIcon(QIcon('tennis.png')) self.radioButton.setIconSize(QSize(40, 40)) self.radioButton.toggled.connect(self.on_selected) self.horizontalLayout.addWidget(self.radioButton) self.label = QtWidgets.QLabel(self.groupBox) self.label.setGeometry(QtCore.QRect(20, 80, 419, 77)) self.label.setObjectName("label") self.verticalLayout.addWidget(self.groupBox) self.verticalLayout_2.addLayout(self.verticalLayout) self.retranslateUi(Dialog) QtCore.QMetaObject.connectSlotsByName(Dialog) def retranslateUi(self, Dialog): _translate = QtCore.QCoreApplication.translate Dialog.setWindowTitle(_translate("Dialog", "PyQt5 QRadioButton & QGroupBox")) self.groupBox.setTitle(_translate("Dialog", "What is your favorite sport ?")) self.radioButton_3.setText(_translate("Dialog", "Football")) self.radioButton_2.setText(_translate("Dialog", "Cricket")) self.radioButton.setText(_translate("Dialog", "Tennis")) self.label.setText(_translate("Dialog", "TextLabel")) def on_selected(self): radio_button = self.sender() if radio_button.isChecked(): self.label.setText("You have selected : " + radio_button.text()) if __name__ == "__main__": import sys app = QtWidgets.QApplication(sys.argv) Dialog = QtWidgets.QDialog() ui = Ui_Dialog() ui.setupUi(Dialog) Dialog.show() sys.exit(app.exec_()) |
Run the complete code and this is the result.