当我们使用PyQt5创建GUI界面时,有时想要为标签控件添加阴影效果,以增加界面的美观性和视觉效果。在PyQt5中,可以通过设置QLabel的样式表来添加阴影。下面是具体步骤和示例说明:
步骤
- 导入PyQt5中需要使用的库:
from PyQt5.QtGui import QPainter, QPalette, QColor, QFont, QPixmap, QImage, QIcon
from PyQt5.QtCore import *
from PyQt5.QtWidgets import *
- 创建一个QLabel对象,并设置对象的显式文本和大小:
label = QLabel('Hello, world!')
label.setFixedSize(200, 100)
- 为标签控件添加阴影效果,需要设置控件的样式表:
label.setStyleSheet("QLabel { background-color : white; color : black; font-size: 20px; border-radius: 6px; border: 2px solid gray; padding: 10px;}"
"QLabel:hover {background-color : #f0f0f0;}"
"QLabel {background-color: white; border: 2px solid gray; color: black; padding: 10px;border-radius: 6px;}"
"QLabel {"
" border-style: outset;"
" border-width: 2px;"
" border-color: beige;"
" background-color: rgb(255, 255, 255);"
" border-radius: 10px;"
" padding: 6px;"
"}"
"QLabel:pressed {"
" background-color: rgb(224, 0, 0);"
" border-style: inset;"
"}")
- 最后将标签控件添加到主窗口或布局中即可。
示例说明
示例一
以下是一个简单的示例,可以创建一个添加了阴影效果的标签控件并将其添加到主窗口中:
import sys
from PyQt5.QtGui import QPainter, QPalette, QColor, QFont, QPixmap, QImage, QIcon
from PyQt5.QtCore import *
from PyQt5.QtWidgets import *
class MainWindow(QWidget):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
label = QLabel('Hello, world!')
label.setFixedSize(200, 100)
label.setStyleSheet("QLabel {"
" border-style: outset;"
" border-width: 2px;"
" border-color: beige;"
" background-color: rgb(255, 255, 255);"
" border-radius: 10px;"
" padding: 6px;"
" color : black;"
"}"
"QLabel:pressed {"
" background-color: rgb(224, 0, 0);"
" border-style: inset;"
"}")
hbox = QHBoxLayout()
hbox.addWidget(label)
self.setLayout(hbox)
self.setGeometry(300, 300, 350, 250)
self.setWindowTitle('PyQt5 Shadow Label')
self.show()
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = MainWindow()
sys.exit(app.exec_())
运行上述代码,可以看到一个添加了红色阴影效果的标签控件。
示例二
以下示例演示如何使用PyQt5创建一个具有阴影效果的文本标签,并在鼠标移动到标签上时突出显示:
import sys
from PyQt5.QtGui import QPainter, QPalette, QColor, QFont, QPixmap, QImage, QIcon
from PyQt5.QtCore import *
from PyQt5.QtWidgets import *
class ShadowLabel(QLabel):
def __init__(self, text):
super().__init__()
self.text = text
self.setFixedSize(200, 100)
self.initUI()
def initUI(self):
self.setText(self.text)
self.setStyleSheet("QLabel {"
" border-style: outset;"
" border-width: 2px;"
" border-color: beige;"
" background-color: rgb(255, 255, 255);"
" border-radius: 10px;"
" padding: 6px;"
" color : black;"
"}"
"QLabel:hover {"
"background-color : #f0f0f0;"
"}"
"QLabel:pressed {"
" background-color: rgb(224, 0, 0);"
" border-style: inset;"
"}")
def enterEvent(self, event):
self.setStyleSheet("QLabel {"
" border-style: outset;"
" border-width: 2px;"
" border-color: #096192;"
" background-color: rgb(255, 255, 255);"
" border-radius: 10px;"
" padding: 6px;"
" color : black;"
"}"
"QLabel:hover {"
"background-color : #f0f0f0;"
"}"
"QLabel:pressed {"
" background-color: rgb(224, 0, 0);"
" border-style: inset;"
"}")
def leaveEvent(self, event):
self.setStyleSheet("QLabel {"
" border-style: outset;"
" border-width: 2px;"
" border-color: beige;"
" background-color: rgb(255, 255, 255);"
" border-radius: 10px;"
" padding: 6px;"
" color : black;"
"}"
"QLabel:hover {"
"background-color : #f0f0f0;"
"}"
"QLabel:pressed {"
" background-color: rgb(224, 0, 0);"
" border-style: inset;"
"}")
if __name__ == '__main__':
app = QApplication(sys.argv)
label = ShadowLabel('PyQt5 Shadow Label')
hbox = QHBoxLayout()
hbox.addWidget(label)
main_window = QWidget()
main_window.setLayout(hbox)
main_window.setGeometry(300, 300, 350, 250)
main_window.setWindowTitle('PyQt5 Shadow Label')
main_window.show()
sys.exit(app.exec_())
在运行上述代码后,可以看到一个添加了阴影效果的文本标签,当鼠标移动到标签上时,标签会突出显示,而当鼠标移开时,标签会恢复原始状态。
本站文章如无特殊说明,均为本站原创,如若转载,请注明出处:PyQt5标签 – 添加阴影 - Python技术站