-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathrainbow-synchronization-example.py
104 lines (85 loc) · 3.34 KB
/
rainbow-synchronization-example.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
import sys
import os
try:
from PyQt6.QtWidgets import (
QApplication,
QProgressBar,
QVBoxLayout,
QWidget,
QPushButton,
)
from PyQt6.QtCore import Qt, QTimer
except ImportError:
os.system("pip install PyQt6")
from PyQt6.QtWidgets import (
QApplication,
QProgressBar,
QVBoxLayout,
QWidget,
QPushButton,
)
from PyQt6.QtCore import Qt, QTimer
try:
import qdarktheme
except ImportError:
os.system("pip install pyqtdarktheme==2.1.0 --ignore-requires-python")
import qdarktheme
from hPyT import rainbow_title_bar
class RainbowProgressBar(QProgressBar):
def __init__(self, *args, interval=int, **kwargs):
super().__init__(*args, **kwargs)
self.timer = QTimer()
self.timer.timeout.connect(self.update_color)
self.timer.start(interval)
def update_color(self):
# get current title bar color from the rainbow_title_bar
r, g, b = rainbow_title_bar.get_current_color()
# set the current title bar color as the current progressbar color
self.setStyleSheet(
f"QProgressBar::chunk {{ background-color: rgb({r}, {g}, {b}); }}"
)
class MainWindow(QWidget):
def __init__(self):
super().__init__()
# initialize the window
self.setWindowTitle('"Rainbow ProgressBar" hPyT Sync Example')
self.setGeometry(100, 100, 600, 400)
qdarktheme.setup_theme(
theme="dark", custom_colors={"[dark]": {"primary": "#FFFFFF"}}
)
self.interval = 30 # define the interval for color updates
window = self.window() # define the window for the rainbow title bar
# Start the rainbow title bar effect for the winow
rainbow_title_bar.start(window, interval=self.interval)
# initialize the layout
layout = QVBoxLayout()
self.setLayout(layout)
# initialize the custom color changing progress bar class
self.progress_bar = RainbowProgressBar(
self, interval=self.interval
) # in order to synchronize the colors make sure the rainbow title bar and your custom color changing widget (here: a progressbar) use the same interval for color updates
self.progress_bar.setGeometry(50, 50, 500, 50)
self.progress_bar.setRange(0, 100)
# create a button which starts a progress simulation for the progressbar
self.start_button = QPushButton("start")
self.start_button.clicked.connect(self.start_progress_simulation)
# define the layout
layout.addStretch()
layout.addWidget(self.progress_bar, alignment=Qt.AlignmentFlag.AlignVCenter)
layout.addWidget(self.start_button, alignment=Qt.AlignmentFlag.AlignVCenter)
layout.addStretch()
# initialize a timer for the progressbar-progress simulation
self.progress_timer = QTimer()
self.progress_timer.timeout.connect(self.progress_simulation)
def start_progress_simulation(self):
self.pb_value = 0
progress_update_interval = 500
self.progress_timer.start(progress_update_interval)
def progress_simulation(self):
self.pb_value += 5
self.progress_bar.setValue(self.pb_value)
if __name__ == "__main__":
app = QApplication(sys.argv)
window = MainWindow()
window.show()
sys.exit(app.exec())