-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclock.py
135 lines (94 loc) · 2.83 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
# importing libraries
from PyQt5.QtWidgets import *
from PyQt5 import QtCore, QtGui
from PyQt5.QtGui import *
from PyQt5.QtCore import *
import sys
# creating a clock class
class Clock(QMainWindow):
# constructor
def __init__(self):
super().__init__()
# creating a timer object
timer = QTimer(self)
# adding action to the timer
# update the whole code
timer.timeout.connect(self.update)
# setting start time of timer i.e 1 second
timer.start(1000)
# setting window title
self.setWindowTitle('Clock')
# setting window geometry
self.setGeometry(200, 200, 300, 300)
# setting background color to the window
self.setStyleSheet("background : black;")
# creating hour hand
self.hPointer = QtGui.QPolygon([QPoint(6, 7),
QPoint(-6, 7),
QPoint(0, -50)])
# creating minute hand
self.mPointer = QPolygon([QPoint(6, 7),
QPoint(-6, 7),
QPoint(0, -70)])
# creating second hand
self.sPointer = QPolygon([QPoint(1, 1),
QPoint(-1, 1),
QPoint(0, -90)])
# colors
# color for minute and hour hand
self.bColor = Qt.green
# color for second hand
self.sColor = Qt.red
# method for paint event
def paintEvent(self, event):
# getting minimum of width and height
# so that clock remain square
rec = min(self.width(), self.height())
# getting current time
tik = QTime.currentTime()
# creating a painter object
painter = QPainter(self)
# method to draw the hands
# argument : color rotation and which hand should be pointed
def drawPointer(color, rotation, pointer):
# setting brush
painter.setBrush(QBrush(color))
# saving painter
painter.save()
# rotating painter
painter.rotate(rotation)
# draw the polygon i.e hand
painter.drawConvexPolygon(pointer)
# restore the painter
painter.restore()
# tune up painter
painter.setRenderHint(QPainter.Antialiasing)
# translating the painter
painter.translate(self.width() / 2, self.height() / 2)
# scale the painter
painter.scale(rec / 200, rec / 200)
# set current pen as no pen
painter.setPen(QtCore.Qt.NoPen)
# draw each hand
drawPointer(self.bColor, (30 * (tik.hour() + tik.minute() / 60)), self.hPointer)
drawPointer(self.bColor, (6 * (tik.minute() + tik.second() / 60)), self.mPointer)
drawPointer(self.sColor, (6 * tik.second()), self.sPointer)
# drawing background
painter.setPen(QPen(self.bColor))
# for loop
for i in range(0, 60):
# drawing background lines
if (i % 5) == 0:
painter.drawLine(87, 0, 97, 0)
# rotating the painter
painter.rotate(6)
# ending the painter
painter.end()
# Driver code
if __name__ == '__main__':
app = QApplication(sys.argv)
# creating a clock object
win = Clock()
# show
win.show()
exit(app.exec_())