|
| 1 | +# PyQt5 Custom Widgets # |
| 2 | +# GPL 3.0 - Kadir Aksoy # |
| 3 | +# https://github.com/kadir014/pyqt5-custom-widgets # |
| 4 | +# # |
| 5 | +# This script is one of the pyqt5Custom examples # |
| 6 | + |
| 7 | + |
| 8 | +import sys |
| 9 | + |
| 10 | +from PyQt5.QtCore import Qt |
| 11 | +from PyQt5.QtWidgets import QWidget, QApplication, QVBoxLayout |
| 12 | +from PyQt5.QtGui import QColor, QFontDatabase, QBrush, QPalette, QLinearGradient |
| 13 | + |
| 14 | +from pyqt5Custom import TitleBar |
| 15 | + |
| 16 | + |
| 17 | + |
| 18 | +class MainWindow(QWidget): |
| 19 | + def __init__(self): |
| 20 | + super().__init__() |
| 21 | + |
| 22 | + QFontDatabase.addApplicationFont("data/SFPro.ttf") |
| 23 | + |
| 24 | + self.setGeometry(100, 100, 410, 240) |
| 25 | + |
| 26 | + self.layout = QVBoxLayout() |
| 27 | + self.layout.setAlignment(Qt.AlignTop) |
| 28 | + self.setLayout(self.layout) |
| 29 | + self.layout.setContentsMargins(0, 0, 0, 0) |
| 30 | + |
| 31 | + self.titlebar = TitleBar(self, title="Custom TitleBar!") |
| 32 | + self.layout.addWidget(self.titlebar, alignment=Qt.AlignTop) |
| 33 | + self.titlebar.setStyleDict({ |
| 34 | + "background-color" : (255, 255, 255), |
| 35 | + "font-size" : 18, |
| 36 | + "font-subpixel-aa" : True, |
| 37 | + "font-family" : "SF Pro Display", |
| 38 | + }) |
| 39 | + |
| 40 | + self.titlebar.closeButton.setStyleDict({ |
| 41 | + "border-radius" : 100, |
| 42 | + "background-color" : (255, 255, 255, 120), |
| 43 | + "font-size" : 18, |
| 44 | + "font-family" : "SF Pro Display", |
| 45 | + "render-fast" : True |
| 46 | + }) |
| 47 | + self.titlebar.maxButton.copyStyleDict(self.titlebar.closeButton) |
| 48 | + self.titlebar.minButton.copyStyleDict(self.titlebar.closeButton) |
| 49 | + |
| 50 | + |
| 51 | + self.anim = self.titlebar.newAnimation() |
| 52 | + self.anim.speed = 0.7 |
| 53 | + |
| 54 | + @self.anim.tick |
| 55 | + def callback(): |
| 56 | + r = QColor(255, 100, 100) |
| 57 | + g = QColor(100, 255, 100) |
| 58 | + b = QColor(100, 100, 255) |
| 59 | + |
| 60 | + if self.anim.current() < 0.25: |
| 61 | + c = self.anim.lerp(r, g) |
| 62 | + elif self.anim.current() < 0.75: |
| 63 | + c = self.anim.lerp(g, b) |
| 64 | + else: |
| 65 | + c = self.anim.lerp(r, b) |
| 66 | + |
| 67 | + self.titlebar.setStyleDict({ |
| 68 | + "background-color" : (c.red(), c.green(), c.blue()), |
| 69 | + }) |
| 70 | + |
| 71 | + self.anim.start(loop = True) |
| 72 | + |
| 73 | + |
| 74 | + |
| 75 | +if __name__ == "__main__": |
| 76 | + app = QApplication(sys.argv) |
| 77 | + |
| 78 | + mw = MainWindow() |
| 79 | + mw.show() |
| 80 | + |
| 81 | + sys.exit(app.exec_()) |
0 commit comments