-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
121 lines (82 loc) · 3.08 KB
/
app.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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
import dataclasses
import logging
import sys
from PyQt5 import QtCore, QtGui, QtWidgets
from core.config import *
from core.style import load_style
from core.utils import resource
from core.weather import WeatherData, OpenWeatherMapWeatherService
from widgets.todayWeatherWidget import TodayWeatherWidget
from widgets.locationWidget import LocationWidget
from widgets.infoWidget import InfoWidget
try: # change app id for correct icon present
from PyQt5.QtWinExtras import QtWin
myAppID = f'{ORGANIZATIONNAME}.{APPLICATIONNAME}.MAINWINDOW.{APPLICATIONVERSION}'
QtWin.setCurrentProcessExplicitAppUserModelID(myAppID)
except ImportError:
pass
WEATHER_SERVICE = OpenWeatherMapWeatherService()
class UpdateWeatherThread(QtCore.QThread):
updated = QtCore.pyqtSignal(WeatherData)
def __init__(self):
super().__init__()
def run(self):
data = WEATHER_SERVICE.get()
self.updated.emit(data)
class CentralWidget(QtWidgets.QFrame):
def __init__(self):
super().__init__()
layout = QtWidgets.QVBoxLayout(self)
layout.setContentsMargins(0, 0, 0, 0)
layout.setSpacing(0)
layout.addWidget(LocationWidget())
layout.addWidget(TodayWeatherWidget())
layout.addWidget(InfoWidget())
def update(self, data: WeatherData):
widget = self.findChild(QtWidgets.QWidget, 'todayWeatherWidget')
widget.update(data)
class MainWindow(QtWidgets.QMainWindow):
def __init__(self, flags):
super().__init__(flags=flags)
self.setWindowFlag(QtCore.Qt.FramelessWindowHint)
self.setAttribute(QtCore.Qt.WA_TranslucentBackground, True)
# icon
icon = QtGui.QIcon(resource('icon.ico'))
self.setWindowIcon(icon)
# styles
style = load_style()
self.setStyleSheet(style)
# central widget
self.centralWidget = CentralWidget()
self.setCentralWidget(self.centralWidget)
# update weather thread
self.updateWeatherThread = UpdateWeatherThread()
self.updateWeatherThread.updated.connect(self.centralWidget.update)
# update weather timer
self.updateWeatherTimer = QtCore.QTimer()
self.updateWeatherTimer.setInterval(WEATHER_UPDATE_INTERVAL)
self.updateWeatherTimer.timeout.connect(self.updateWeatherThread.start)
self.updateWeatherTimer.start()
QtCore.QTimer.singleShot(300, self.updateWeatherThread.start)
#
self.show()
def mousePressEvent(self, event):
self._beginPos = event.globalPos()
def mouseMoveEvent(self, event):
delta = QtCore.QPoint(event.globalPos() - self._beginPos)
self.move(
self.x() + delta.x(),
self.y() + delta.y(),
)
self._beginPos = event.globalPos()
if __name__ == '__main__':
# logger
if DEBUG:
logger = logging.getLogger('app')
logger.debug('app: run.')
# app
app = QtWidgets.QApplication(sys.argv)
window = MainWindow(
flags=QtCore.Qt.Window | QtCore.Qt.WindowStaysOnTopHint,
)
sys.exit(app.exec())