-
Notifications
You must be signed in to change notification settings - Fork 20
/
ClockDigital.py
146 lines (119 loc) · 3.76 KB
/
ClockDigital.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
import wx
from math import modf
import datetime
now = datetime.datetime.now
def formatTime( s ):
if s < 0:
sgn = '-'
s = -s
else:
sgn = ''
s = int(s)
hours, minutes, seconds = s//(60*60), (s//60)%60, s%60
if hours:
return '{}{:d}:{:02d}:{:02d}'.format( sgn, hours, minutes, seconds )
else:
return '{}{:d}:{:02d}'.format( sgn, minutes, seconds )
class ClockDigital(wx.Control):
def __init__(self, parent, id=wx.ID_ANY, pos=wx.DefaultPosition,
size=wx.DefaultSize, style=wx.NO_BORDER, validator=wx.DefaultValidator,
name="ClockDigital", refTime=None, countDown=False, checkFunc=None ):
super().__init__(parent, id, pos, size, style, validator, name)
self.timer = wx.Timer( self )
self.Bind( wx.EVT_TIMER, self.onTimer )
# Bind the events related to our control: first of all, we use a
# combination of wx.BufferedPaintDC and an empty handler for
# wx.EVT_ERASE_BACKGROUND (see later) to reduce flicker
self.Bind(wx.EVT_PAINT, self.OnPaint)
self.Bind(wx.EVT_ERASE_BACKGROUND, self.OnEraseBackground)
self.Bind(wx.EVT_SIZE, self.OnSize)
self.initialSize = size
self.checkFunc = checkFunc if checkFunc else lambda: True
self.refTime = refTime
self.countDown = countDown
self.tCur = now()
wx.CallAfter( self.onTimer )
def DoGetBestSize(self):
return wx.Size(100, 20) if self.initialSize is wx.DefaultSize else self.initialSize
def SetForegroundColour(self, colour):
super().SetForegroundColour(colour)
self.Refresh()
def SetBackgroundColour(self, colour):
super().SetBackgroundColour(colour)
self.Refresh()
def GetDefaultAttributes(self):
return wx.StaticText.GetClassDefaultAttributes()
def ShouldInheritColours(self):
return True
def SetRefTime( self, refTime ):
self.refTime = refTime
self.Refresh()
def onTimer( self, event=None):
if not self.timer.IsRunning():
self.tCur = now()
self.Refresh()
if self.checkFunc():
if self.refTime:
s = (now() - self.refTime).total_seconds()
if s <= 0.0:
millis = int(modf(-s)[0] * 1000.0)
else:
millis = 1001 - int(modf(s)[0] * 1000.0)
self.timer.Start( millis, True )
else:
self.timer.Start( 1001 - now().microsecond//1000, True )
def Start( self ):
self.onTimer()
def OnPaint(self, event):
dc = wx.BufferedPaintDC(self)
self.Draw(dc)
def OnSize(self, event):
self.Refresh()
event.Skip()
def Draw(self, dc):
dc.Clear()
borderRatio = 0.08
workRatio = (1.0 - borderRatio)
t = self.tCur
size = self.GetClientSize()
width = size.width
height = size.height
if self.refTime:
if self.countDown:
tStr = formatTime( (self.refTime-t).total_seconds() )
else:
tStr = formatTime( (t-self.refTime).total_seconds() )
else:
tStr = t.strftime('%H:%M:%S')
fontSize = int(height * workRatio)
font = wx.Font(
(0,fontSize),
wx.FONTFAMILY_SWISS,
wx.FONTSTYLE_NORMAL,
wx.FONTWEIGHT_NORMAL,
)
dc.SetFont( font )
tWidth, tHeight = dc.GetTextExtent( tStr )
if tWidth > width*workRatio:
fontSize = int( fontSize * width*workRatio / tWidth )
font = wx.Font(
(0,fontSize),
wx.FONTFAMILY_SWISS,
wx.FONTSTYLE_NORMAL,
wx.FONTWEIGHT_NORMAL,
)
dc.SetFont( font )
tWidth, tHeight = dc.GetTextExtent( tStr )
dc.DrawText( tStr, (width-tWidth)//2, (height-tHeight)//2 )
def OnEraseBackground(self, event):
# This is intentionally empty, because we are using the combination
# of wx.BufferedPaintDC + an empty OnEraseBackground event to
# reduce flicker
pass
if __name__ == '__main__':
app = wx.App(False)
mainWin = wx.Frame(None,title="ClockDigital", size=(600,400))
ClockDigital = ClockDigital(mainWin, refTime=now()+datetime.timedelta(seconds=5), countDown=False)
#ClockDigital = ClockDigital(mainWin)
mainWin.Show()
app.MainLoop()