-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclock.py
executable file
·181 lines (138 loc) · 5.15 KB
/
clock.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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
#!/usr/bin/python3
# -*- coding: utf-8 -*-
"""
Created on Fri Jun 23 22:06:01 2017
@author: Zoran Grujić
"""
import sys
import os
import time
from PyQt5 import QtGui, QtWidgets, QtCore
import configparser
dir_ = os.path.dirname(os.path.realpath(sys.argv[0]))
config=configparser.ConfigParser() #make config object
configFile = dir_ + "/config.txt"
class ClockWindow(QtWidgets.QWidget):
width=None
height=None
clockFontSize = 200
talkTime = 20*60 #20 min total time in seconds
talkQuestions = 5*60 #save 5 min for questions in seconds
remainingTime = talkTime
clockRunning = False
def __init__(self, width, height):
super(ClockWindow, self).__init__()
self.width = width
self.height = height
self.getConfig()
self.remainingTime = self.talkTime
self.timer = QtCore.QTimer(self)#make timer to belong to the PlotForm class
self.setStyleSheet("QWidget { background-color: green; }")
self.initUI()
def initUI(self):
"""
font = QtGui.QFont()
font.setFamily(_fromUtf8("FreeMono"))
font.setBold(True)
font.setSize(50)
"""
self.clockLabel = QtWidgets.QLabel("15:00", self)# bottom left to indicate app status
#self.clockLabel.setFont(font)
self.setDisplayTime()
self.helpLabel = QtWidgets.QLabel('SPACE: restart, p: pause/play, LEFT: one minute less, RIGHT: one minute plus, UP: font increase, DOWN: font decrease', self)#
mainVBox = QtWidgets.QVBoxLayout()
mainVBox.addStretch(1)
clockHBox = QtWidgets.QHBoxLayout()
clockHBox.addStretch(1)
clockHBox.addWidget(self.clockLabel)
clockHBox.addStretch(1)
mainVBox.addLayout(clockHBox)
mainVBox.addStretch(1)
mainVBox.addWidget(self.helpLabel)
self.setLayout(mainVBox)
self.setGeometry(150, 150, self.width-300, self.height-300)
self.timer.setInterval( 1000)
self.timer.timeout.connect(self.clockThick)
self.timer.start()
def setDisplayTime(self):
if self.remainingTime > 0:
rmSec = self.remainingTime % 60
rmMin= int((self.remainingTime - rmSec)/60)
else:
rmSec = (-self.remainingTime) % 60
rmMin= int(((-self.remainingTime) - rmSec)/60)
self.clockLabel.setText("{0:02d}:{1:02d}".format(rmMin, rmSec) )
self.clockLabel.setStyleSheet("font: %spt Comic Sans MS" % self.clockFontSize)
def clockThick(self):
if self.clockRunning:
#decrease remaining time
self.remainingTime = self.remainingTime - 1
self.setDisplayTime()
#set color, green
if self.remainingTime> self.talkQuestions:
self.setBgColor("green")
else:
if self.remainingTime > 0:
self.setBgColor("orange")
else:
self.setBgColor("red")
else:
return
pass
def getConfig(self):
"""
Read configuration
"""
global config
config.read(configFile)
#print(configFile," ",config, " ", config.sections())
self.talkTime = int(eval(config["Talk"]["talktime_sec"]))
self.talkQuestions = int(eval(config["Talk"]["questionstime_sec"]))
#print(self.talkTime, " ", self.talkQuestions)
def setBgColor(self, color):
self.setStyleSheet("QWidget { background-color: %s; }" % color)
def keyPressEvent(self, event):
key = event.key()
#print(key)
if key == 32: #space
#restart
self.getConfig()
self.clockRunning = True
self.remainingTime = self.talkTime
return
if key == 80: # p for pause/play
self.clockRunning = not self.clockRunning
return
if key == QtCore.Qt.Key_Left:
self.remainingTime = self.remainingTime + 60
self.setDisplayTime()
#print('Left Arrow Pressed')
return
if key == QtCore.Qt.Key_Right:
self.remainingTime = self.remainingTime - 60
self.setDisplayTime()
#print('Right Arrow Pressed')
return
if key == QtCore.Qt.Key_Up:
self.clockFontSize = self.clockFontSize+ 20
self.setDisplayTime()
#print('Up Arrow Pressed')
return
if key == QtCore.Qt.Key_Down:
self.clockFontSize = self.clockFontSize- 20
self.setDisplayTime()
#print('Down Arrow Pressed')
return
def main():
# app = QtGui.QApplication(sys.argv)
app = QtWidgets.QApplication(sys.argv)
screen_resolution = app.desktop().screenGeometry()
wh= [screen_resolution.width(), screen_resolution.height()]
clock = ClockWindow(wh[0],wh[1])
#print("Height: ", wh[1])
clock.show()
#clock.showMaximized()
app.exec_()#execute until closed
exit()
if __name__ == '__main__':
main()