简体   繁体   中英

How to add bottom shadow effect in pyqt5?

I've been planing to style my QPushButton like this

Any suggestion?

Set a color that's a bit darker than the background-color to border-bottom in a style sheet.

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_())

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM