-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsleep-time-gui.py
More file actions
130 lines (110 loc) · 4.47 KB
/
sleep-time-gui.py
File metadata and controls
130 lines (110 loc) · 4.47 KB
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
import os
import threading
import time
from datetime import datetime, timedelta
import sys
import json
from PyQt5.QtWidgets import QApplication, QWidget, QPushButton, QMenu, QMenuBar
from PyQt5 import QtCore, QtGui, QtWidgets
from ui import main
class MyQtApp(main.Ui_MainWindow, QtWidgets.QMainWindow):
def __init__(self):
super(MyQtApp, self).__init__()
self.setupUi(self)
self.setWindowTitle("Sleep Timer")
# Timer starten
self.timer = None
# Dark mode & StyleSheet
self.dark_mode = True
self.load_config()
self.stylesheet()
self.action_Dark_Mode.triggered.connect(self.set_dark_mode)
self.action_Light_Mode.triggered.connect(self.set_light_mode)
# Signale und Slots verbinden
self.cancel_button.clicked.connect(self.cancel_timer)
self.exit_button.clicked.connect(self.cancel_timer)
self.two_hours_button.clicked.connect(lambda: self.start_timer(2 * 60 * 60))
self.one_hour_button.clicked.connect(lambda: self.start_timer(1 * 60 * 60))
self.thirty_min_button.clicked.connect(lambda: self.start_timer(30 * 60))
def start_timer(self, duration):
# Wenn bereits ein Timer läuft, diesen zuerst abbrechen
if self.timer:
self.timer.cancel()
# Timer starten
self.timer = CountdownTimer(duration, self)
self.timer.start()
def cancel_timer(self):
# Timer abbrechen
if self.timer:
self.timer.cancel()
self.timer = None
self.time_label.setText("Timer Abgebrochen, neue Zeit wählen.")
def set_time_label(self, time_str):
self.time_label.setText("Heruntergefahren in: {} um {} Uhr.".format(time_str, (datetime.now() + timedelta(seconds=self.timer.duration)).strftime("%H:%M:%S")))
def stylesheet(self):
# StyleSheet
for button in self.findChildren(QPushButton):
button.setStyleSheet("QPushButton:hover { background-color: rgba(135, 167, 82, 100%); border: 1px solid #00FF00; }")
for qmenu in self.findChildren(QMenu):
qmenu.setStyleSheet("QMenu::item:selected { background-color: rgba(135, 167, 82, 100%); border: 1px solid #00FF00; color: #fff; }")
for qmenubar in self.findChildren(QMenuBar):
qmenubar.setStyleSheet("QMenuBar::item:selected { background-color: rgba(135, 167, 82, 100%); border: 1px solid #00FF00; color: #fff; }")
def set_dark_mode(self):
# Dark mode aktivieren
self.dark_mode = True
self.setStyleSheet("background-color: #222222; color: #ffffff;")
self.save_config()
def set_light_mode(self):
# Light mode aktivieren
self.dark_mode = False
self.setStyleSheet("background-color: #ffffff; color: #000000;")
self.save_config()
def load_config(self):
try:
with open("config.json", "r") as f:
config = json.load(f)
self.dark_mode = config["dark_mode"]
if self.dark_mode:
self.set_dark_mode()
else:
self.set_light_mode()
except FileNotFoundError:
pass
def save_config(self):
config = {"dark_mode": self.dark_mode}
with open("config.json", "w") as f:
json.dump(config, f)
def closeEvent(self, event):
self.cancel_timer()
self.save_config()
event.accept()
class CountdownTimer:
def __init__(self, duration, ui):
self.duration = duration
self.ui = ui
self.end_time = datetime.now() + timedelta(seconds=duration)
self.timer = None
self.cancelled = False
def start(self):
self.timer = threading.Thread(target=self.run)
self.timer.start()
def run(self):
try:
while self.duration and not self.cancelled:
hours, remainder = divmod(self.duration, 3600)
minutes, seconds = divmod(remainder, 60)
time_str = "{:02d}:{:02d}:{:02d}".format(hours, minutes, seconds)
self.ui.set_time_label(time_str)
time.sleep(1)
self.duration -= 1
if not self.cancelled:
os.system("shutdown -h now")
except KeyboardInterrupt:
print("\nTimer Abgebrochen")
def cancel(self):
self.cancelled = True
if __name__ == "__main__":
app = QApplication(sys.argv)
qt_app = MyQtApp()
qt_app.show()
sys.exit(app.exec_())