[英]How to add bottom shadow effect in pyqt5?
I've been planing to style my QPushButton like this我一直计划像这样设计我的QPushButton
Any suggestion?有什么建议吗?
Set a color that's a bit darker than the background-color
to border-bottom
in a style sheet.在样式表中将比
background-color
深一点的颜色设置为border-bottom
。
import sys
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
class Button(QPushButton):
def __init__(self, color, *args, **kwargs):
super().__init__(*args, **kwargs)
color = QColor(color)
shadow = color.darker(115).name()
text = 'black' if color.value() > 186 else 'white'
self.setStyleSheet(f'''
QPushButton {{
color: {text};
background-color: {color.name()};
padding: 12px;
border-radius: 4px;
border-bottom: 4px solid {shadow};
}}''')
if __name__ == '__main__':
app = QApplication(sys.argv)
window = QScrollArea(widgetResizable=True)
w = QWidget(); window.setWidget(w)
grid = QGridLayout(w, spacing=40)
colors = QColor.colorNames(); colors.remove('transparent')
for i, v in enumerate(colors):
grid.addWidget(Button(v, f'{v} button'.title()), *divmod(i, 7))
window.show()
sys.exit(app.exec_())
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.