Skip to content
Related Articles

Related Articles

Improve Article

PyQt5 QRadioButton

  • Last Updated : 21 May, 2021

Radio button has a circular shape and used when one choice is to be selected from multiple options. This button is in the selected state when the circle is filled and deselected when the circle is hollow. QRadioButton class is used to add a radio button in an application.

A window asking the user to select user’s gender. According to the user’s choice, it shows a message: “You are male/female” and which will change if the user changes his/her selection.
Below is the code: 


import sys
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
        MainWindow.resize(466, 299)
        self.centralwidget = QtWidgets.QWidget(MainWindow)
        # Radio button for male
        self.radioButton_male = QtWidgets.QRadioButton(self.centralwidget)
        self.radioButton_male.setGeometry(QtCore.QRect(180, 120, 95, 20))
        # adding signal and slot
        # Radio button for female
        self.radioButton_female = QtWidgets.QRadioButton(self.centralwidget)
        self.radioButton_female.setGeometry(QtCore.QRect(180, 150, 95, 20))
        # adding signal and slot
        self.label = QtWidgets.QLabel(self.centralwidget)
        self.label.setGeometry(QtCore.QRect(170, 90, 211, 20))
    def maleselected(self, selected):
        if selected:
            self.label.setText("You are male")
    def femaleselected(self, selected):
        if selected:
            self.label.setText("You are female")       
    def retranslateUi(self, MainWindow):
        _translate = QtCore.QCoreApplication.translate
        MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
        self.radioButton_male.setText(_translate("MainWindow", "Male"))
        self.label.setText(_translate("MainWindow", "Select your gender:"))
        self.radioButton_female.setText(_translate("MainWindow", "Female"))
# Driver Code
if __name__ == "__main__":
    app = QtWidgets.QApplication(sys.argv)
    MainWindow = QtWidgets.QMainWindow()
    ui = Ui_MainWindow()


According to user’s choice a message will appear “You are male” or “You are female” and which will change as user changes his/her selection. 


 Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.  

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning – Basic Level Course

My Personal Notes arrow_drop_up
Recommended Articles
Page :