PyQt5 QSpinBox – Setting background role

In this article we will see how we can set the background role to the spin box, there are basically two types of roles in spin box one is foreground and the other one is background. Background role defines the brush from the spin box’s palette that is used to render the background. If no explicit background role is set, the spin box inherits its parent widget’s background role.
In order to do this we use setBackgroundRole method
Syntax : spin_box.setBackgroundRole(color_object)
Argument : It takes QPalette.ColorRole object argument
Return : It returns None
Below is the implementation
Python3
| # importing librariesfromPyQt5.QtWidgets import*fromPyQt5 importQtCore, QtGuifromPyQt5.QtGui import*fromPyQt5.QtCore import*importsysclassWindow(QMainWindow):    def__init__(self):        super().__init__()        # setting title        self.setWindowTitle("Python ")        # setting geometry        self.setGeometry(100, 100, 600, 400)        # calling method        self.UiComponents()        # showing all the widgets        self.show()    # method for widgets    defUiComponents(self):        # creating spin box        self.spin =QSpinBox(self)        # setting geometry to spin box        self.spin.setGeometry(100, 100, 250, 40)        # setting prefix to spin        self.spin.setPrefix("Prefix ")        # setting suffix to spin        self.spin.setSuffix(" Suffix")        # making auto fill background of spin box        self.spin.setAutoFillBackground(True)        # setting background role        self.spin.setBackgroundRole(QPalette.Base)        # getting palette and setting color to the background to it        p =self.spin.palette()        p.setColor(self.spin.backgroundRole(), Qt.darkGray)        self.spin.setPalette(p)        # create pyqt5 appApp =QApplication(sys.argv)# create the instance of our Windowwindow =Window()# start the appsys.exit(App.exec()) | 
Output :
 
				 
					



