-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
137 lines (105 loc) · 4.66 KB
/
Copy pathmain.py
File metadata and controls
137 lines (105 loc) · 4.66 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
131
132
133
134
135
136
137
import os
import sys
from PyQt5 import QtWidgets, QtCore, QtGui, QtWebEngineWidgets
class WeatherOverlay(QtWidgets.QMainWindow):
def __init__(self, html_path):
super().__init__()
self.html_path = html_path
self.always_on_top = True
self.setup_ui()
self.setup_tray()
def setup_ui(self):
flags = QtCore.Qt.FramelessWindowHint | QtCore.Qt.Tool
if self.always_on_top:
flags |= QtCore.Qt.WindowStaysOnTopHint
self.setWindowFlags(flags)
self.setAttribute(QtCore.Qt.WA_TranslucentBackground, True)
self.resize(450, 260)
self.view = QtWebEngineWidgets.QWebEngineView(self)
self.view.setAttribute(QtCore.Qt.WA_TranslucentBackground, True)
self.view.page().setBackgroundColor(QtGui.QColor(0, 0, 0, 0))
self.view.load(QtCore.QUrl.fromLocalFile(self.html_path))
self.setCentralWidget(self.view)
self.drag_layer = DragOverlay(self)
self.drag_layer.setGeometry(self.rect())
self.drag_layer.raise_()
def setup_tray(self):
self.tray_icon = QtWidgets.QSystemTrayIcon(self)
icon_path = os.path.join(os.path.dirname(__file__), "app-icon.png")
if os.path.exists(icon_path):
self.tray_icon.setIcon(QtGui.QIcon(icon_path))
else:
# Способ 2: Создание простой иконки программно
pixmap = QtGui.QPixmap(32, 32)
pixmap.fill(QtGui.QColor(0, 100, 200)) # Синий цвет
self.tray_icon.setIcon(QtGui.QIcon(pixmap))
# Create menu
tray_menu = QtWidgets.QMenu()
self.toggle_top_action = tray_menu.addAction(
"Disable always-on-top" if self.always_on_top else "Enable always-on-top"
)
self.toggle_top_action.triggered.connect(self.toggle_always_on_top)
self.toggle_drag_action = tray_menu.addAction(
"Disable drag" if self.drag_layer.drag_enabled else "Enable drag"
)
self.toggle_drag_action.triggered.connect(self.toggle_drag)
quit_action = tray_menu.addAction("Quit")
quit_action.triggered.connect(self.quit_app)
self.tray_icon.setContextMenu(tray_menu)
self.tray_icon.show()
def update_tray_menu(self):
self.toggle_top_action.setText(
"Disable always-on-top" if self.always_on_top else "Enable always-on-top"
)
self.toggle_drag_action.setText(
"Disable drag" if self.drag_layer.drag_enabled else "Enable drag"
)
def toggle_always_on_top(self):
self.always_on_top = not self.always_on_top
geometry = self.geometry()
flags = QtCore.Qt.FramelessWindowHint | QtCore.Qt.Tool
if self.always_on_top:
flags |= QtCore.Qt.WindowStaysOnTopHint
self.hide()
self.setWindowFlags(flags)
self.setGeometry(geometry)
self.show()
self.update_tray_menu()
def toggle_drag(self):
self.drag_layer.drag_enabled = not self.drag_layer.drag_enabled
self.update_tray_menu()
def quit_app(self):
self.tray_icon.hide()
self.close()
QtWidgets.QApplication.quit()
def resizeEvent(self, event):
super().resizeEvent(event)
self.drag_layer.setGeometry(self.rect())
class DragOverlay(QtWidgets.QWidget):
def __init__(self, parent=None):
super().__init__(parent)
self._drag_pos = None
self.drag_enabled = True
def mousePressEvent(self, event):
if self.drag_enabled and event.button() == QtCore.Qt.LeftButton:
self._drag_pos = event.globalPos() - self.parentWidget().frameGeometry().topLeft()
def mouseMoveEvent(self, event):
if self.drag_enabled and self._drag_pos is not None and event.buttons() & QtCore.Qt.LeftButton:
self.parentWidget().move(event.globalPos() - self._drag_pos)
def mouseReleaseEvent(self, event):
self._drag_pos = None
if __name__ == "__main__":
QtCore.QCoreApplication.setAttribute(QtCore.Qt.AA_EnableHighDpiScaling)
QtCore.QCoreApplication.setAttribute(QtCore.Qt.AA_UseHighDpiPixmaps)
app = QtWidgets.QApplication(sys.argv)
if not QtWidgets.QSystemTrayIcon.isSystemTrayAvailable():
print("System tray is not available")
sys.exit(1)
app.setQuitOnLastWindowClosed(False)
html_path = os.path.abspath("index.html")
if not os.path.exists(html_path):
print(f"Error: HTML file not found at {html_path}")
sys.exit(1)
overlay = WeatherOverlay(html_path)
overlay.show()
sys.exit(app.exec_())