PyQt5 QSpinBox – 为向下箭头添加皮肤
在本文中,我们将了解如何将皮肤设置为旋转框的向下箭头以获取其各种状态。旋转框有两个孩子,一个是行编辑,另一个是向上和向下按钮。向下箭头是向下按钮的内部组件,基本上有三种状态,一种是正常状态,第二种是悬停状态,即光标在向下箭头上时,第三种是按下状态。皮肤基本上是一个背景图像,可以根据旋转框的大小自行调整。
为此,我们必须更改与旋转框关联的样式表,下面是样式表代码
QSpinBox::down-arrow
{
border-image : url(image1.png);
}
QSpinBox::down-arrow:hover
{
border-image : url(image2.png);
}
QSpinBox::down-arrow:pressed
{
border-image : url(image3.png);
}
这将为向下箭头的每个状态添加三个不同的皮肤,还有一些额外的状态,如反悬停 (!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 skin to spin box's down-arrow
# adding skin to down arrow for hover and pressed state
self.spin.setStyleSheet("QSpinBox::down-arrow"
"{"
"border-image : url(image.png);"
"}"
"QSpinBox::down-arrow:hover"
"{"
"border-image : url(skin.png);"
"}"
"QSpinBox::down-arrow:pressed"
"{"
"border-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())
输出 :