-
Notifications
You must be signed in to change notification settings - Fork 1
/
Window.py
159 lines (112 loc) · 4.21 KB
/
Window.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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
import sys
from ZoneInfo import ZoneInfo
from ZoneWidget import ZoneWidget
from PySide2 import QtWidgets, QtGui, QtCore
import Constants
import os
import Config
app = QtWidgets.QApplication(sys.argv)
class Window(QtWidgets.QMainWindow):
def __init__(self):
super().__init__()
self.timer_count = 0
font = ""
for file in os.listdir(os.getcwd()):
if file.endswith(".ttf"):
font = file
break
if font != "":
QtGui.QFontDatabase.addApplicationFont(font)
grid = QtWidgets.QGridLayout()
top_set = QtWidgets.QHBoxLayout()
centre_set = QtWidgets.QHBoxLayout()
bottom_set = QtWidgets.QHBoxLayout()
top_set.setAlignment(QtCore.Qt.AlignHCenter | QtCore.Qt.AlignBottom)
bottom_set.setAlignment(QtCore.Qt.AlignHCenter | QtCore.Qt.AlignTop)
width = 0
height = 0
for zone in Config.TOP_ROW_LOCATIONS:
new_widget = ZoneWidget(ZoneInfo(zone))
new_size = new_widget.get_size()
if new_size.width() > width:
width = new_size.width()
if new_size.height() > height:
height = new_size.height()
top_set.addWidget(new_widget)
for zone in Config.CENTRE_ROW_LOCATIONS:
new_widget = ZoneWidget(ZoneInfo(zone))
new_size = new_widget.get_size()
if new_size.width() > width:
width = new_size.width()
if new_size.height() > height:
height = new_size.height()
centre_set.addWidget(new_widget)
for zone in Config.BOTTOM_ROW_LOCATIONS:
new_widget = ZoneWidget(ZoneInfo(zone))
new_size = new_widget.get_size()
if new_size.width() > width:
width = new_size.width()
if new_size.height() > height:
height = new_size.height()
bottom_set.addWidget(new_widget)
top_widget = QtWidgets.QWidget()
top_widget.setLayout(top_set)
centre_widget = QtWidgets.QWidget()
centre_widget.setLayout(centre_set)
bottom_widget = QtWidgets.QWidget()
bottom_widget.setLayout(bottom_set)
if not Config.TOP_ROW_LOCATIONS:
top_widget.hide()
if not Config.CENTRE_ROW_LOCATIONS:
centre_widget.hide()
if not Config.BOTTOM_ROW_LOCATIONS:
bottom_widget.hide()
grid.addWidget(top_widget, 0, 0)
grid.addWidget(centre_widget, 1, 0)
grid.addWidget(bottom_widget, 2, 0)
bottom_set.setSpacing(15)
top_set.setSpacing(15)
self.win = QtWidgets.QWidget()
self.win.setWindowTitle("PyClock")
self.win.setSizePolicy(QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Preferred, QtWidgets.QSizePolicy.Preferred))
self.win.keyPressEvent = self.keyPressEvent
self.set_css()
self.win.setLayout(grid)
# Data Timer
self.timer = QtCore.QTimer()
self.timer.timeout.connect(self.update_data)
self.timer.start(1000)
for item in self.win.findChildren(ZoneWidget):
item.update_size(width, height)
def update_data(self):
self.timer_count += 1
reset_timer = False
for control in self.win.findChildren(ZoneWidget):
control.update_times()
if self.timer_count == Constants.WEATHER_UPDATE_INTERVAL:
control.update_weather()
reset_timer = True
if reset_timer:
self.timer_count = 0
def keyPressEvent(self, event):
if event.key() == Constants.ESCAPE_KEY:
self.win.close()
def start(self):
self.win.showFullScreen()
app.exec_()
def set_css(self):
self.win.setStyleSheet(
'''
QWidget
{
background-color: % s;
color: % s;
}
.ZoneWidget
{
background-color: % s;
padding-top: 15px;
border-radius: 10px;
}
''' % (Config.BACKGROUND_COLOR, Config.FOREGROUND_COLOR, Config.CARD_COLOR)
)