PyQt5 – Background image to indicator of checked radio button when mouse hover
Last Updated : 22 Apr, 2020
In this article we will see how we can set background image to the indicator of radio button when mouse hover over it and it was in checked state, by default no image is associated with it although we can add image to it. The background image only appear to the indicator when it was in checked state and when mouse hover over the radio button.
In order to add background image to indicator of radio button we have to change the style sheet code of it. Below is the style sheet code.
# importing librariesfromPyQt5.QtWidgetsimport*fromPyQt5importQtCore,QtGuifromPyQt5.QtGuiimport*fromPyQt5.QtCoreimport*importsysclassWindow(QMainWindow):def__init__(self):super().__init__()# setting titleself.setWindowTitle("Python ")# setting geometryself.setGeometry(100,100,600,400)# calling methodself.UiComponents()# showing all the widgetsself.show()# method for widgetsdefUiComponents(self):# creating a radio buttonself.radio_button=QRadioButton(self)# setting geometry of radio buttonself.radio_button.setGeometry(200,150,120,40)# setting text to radio buttonself.radio_button.setText("Radio Button")# changing style sheet code of radio button# setting background image checked indicator when mouse hover over itself.radio_button.setStyleSheet("QRadioButton::indicator:checked:hover""{""background-image : url(image.png);""}")# create pyqt5 appApp=QApplication(sys.argv)# create the instance of our Windowwindow=Window()# start the appsys.exit(App.exec())