📜  PyQt5 QSpinBox - 为多个状态添加背景图片

📅  最后修改于: 2022-05-13 01:55:00.131000             🧑  作者: Mango

PyQt5 QSpinBox - 为多个状态添加背景图片

在本文中,我们将了解如何将背景图像设置为旋转框的各种状态。旋转框基本上有三种状态,一种是正常状态,第二种是悬停状态,即光标在旋转框上时,第三种是按下状态,即按下鼠标按钮。

为此,我们必须更改与旋转框关联的样式表,下面是样式表代码

QSpinBox
{
border : 1px solid black;
background-image : url(image1.png);
}
QSpinBox::hover
{
background-image : url(image2.png);
}
QSpinBox::pressed
{
background-image : url(image3.png);
}

这将为每个状态添加三个不同的背景图像,还有一些额外的状态,如反悬停 (!hover) 和反按下 (!pressed),它们分别与悬停和按下状态相反。

注意:我们必须为正常状态添加边框,否则图像将不会显示,因为 PyQt5 不允许它覆盖样式表

下面是实现

# importing libraries
from PyQt5.QtWidgets import * 
from PyQt5 import QtCore, QtGui
from PyQt5.QtGui import * 
from PyQt5.QtCore import * 
import sys
  
  
class Window(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
    def UiComponents(self):
        # creating spin box
        self.spin = QSpinBox(self)
  
        # setting geometry to spin box
        self.spin.setGeometry(100, 100, 250, 40)
  
        # setting range to the spin box
        self.spin.setRange(0, 9)
  
        # setting prefix to spin
        self.spin.setPrefix("PREFIX ")
  
        # setting suffix to spin
        self.spin.setSuffix(" SUFFIX")
  
        # setting style sheet
        # adding background image
        # adding background image for hover and pressed state
        self.spin.setStyleSheet("QSpinBox"
                                "{"
                                "border : 2px solid black;"
                                "background-image : url(image.png);"
                                "}"
                                "QSpinBox::hover"
                                "{"
                                "background-image : url(skin.png);"
                                "}"
                                "QSpinBox::pressed"
                                "{"
                                "background-image : url(logo.png);"
                                "}"
                                )
  
  
# create pyqt5 app
App = QApplication(sys.argv)
  
# create the instance of our Window
window = Window()
  
# start the app
sys.exit(App.exec())

输出 :