PyQt5 QSpinBox – 为多个状态的向下箭头添加背景图片

PyQt5 QSpinBox – 为多个状态的向下箭头添加背景图片

在这篇文章中,我们将看到如何为旋转框的向下箭头设置不同状态的背景图片。旋转框有两个孩子,一个是行编辑,另一个是向上和向下按钮。向下箭头是向下按钮的内部组件向下箭头是显示箭头的地方,基本上有三种状态,一种是正常状态,第二种是悬停状态,即当光标在向下按钮上时,第三种是按下的状态。

为了做到这一点,我们必须改变与旋转框相关的样式表,下面是样式表的代码

QSpinBox::down-arrow
{
background-image : url(image1.png);
}
QSpinBox::down-arrow:hover
{
background-image : url(image2.png);
}
QSpinBox::down-arrow:pressed
{
background-image : url(image3.png);
}

这将为下箭头的每个状态添加三种不同的背景图片,还有一些额外的状态,如anti-hover(!hover)和anti-pressed(!pressed)这些分别是与hover和pressed状态相反的。

下面是实现方法

# 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, 9999)
  
        # setting prefix to spin
        self.spin.setPrefix("PREFIX ")
  
        # setting suffix to spin
        self.spin.setSuffix(" SUFFIX")
  
        # setting style sheet
        # adding background image to down arrow
        # adding background image to down arrow
        # for hover and pressed state
        self.spin.setStyleSheet("QSpinBox::down-arrow"
                                "{"
                                "background-image : url(image.png);"
                                "}"
                                "QSpinBox::down-arrow:hover"
                                "{"
                                "background-image : url(skin.png);"
                                "}"
                                "QSpinBox::down-arrow: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())

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程

PyQt5 计数器控件QSPINBox