]>
git.saurik.com Git - wxWidgets.git/blob - wxPython/wx/lib/analogclock.py
1 #----------------------------------------------------------------------
2 # Name: wxPython.lib.analogclock
3 # Purpose: A simple analog clock window
5 # Author: several folks on wxPython-users
7 # Created: 16-April-2003
9 # Copyright: (c) 2003 by Total Control Software
10 # Licence: wxWindows license
11 #----------------------------------------------------------------------
13 import math
, sys
, string
, time
14 from wxPython
.wx
import *
18 class AnalogClockWindow(wxWindow
):
19 """A simple analog clock window"""
25 def __init__(self
, parent
, ID
=-1, pos
=wxDefaultPosition
, size
=wxDefaultSize
,
26 style
=0, name
="clock"):
27 # Initialize the wxWindow...
28 wxWindow
.__init
__(self
, parent
, ID
, pos
, size
, style
, name
)
30 # Initialize the default clock settings...
33 self
.tickMarksBrushC
= self
.GetForegroundColour()
34 self
.tickMarksPenC
= self
.GetForegroundColour()
35 self
.tickMarkStyle
= self
.TICKS_SQUARE
37 # Make an initial bitmap for the face, it will be updated and
38 # painted at the first EVT_SIZE event.
40 self
.faceBitmap
= wxEmptyBitmap(max(W
,1), max(H
,1))
42 # Initialize the timer that drives the update of the clock
43 # face. Update every half second to ensure that there is at
44 # least one true update during each realtime second.
45 self
.timer
= wxTimer(self
)
48 # Set event handlers...
49 EVT_PAINT(self
, self
.OnPaint
)
50 EVT_ERASE_BACKGROUND(self
, lambda x
: None)
51 EVT_SIZE(self
, self
.OnSize
)
52 EVT_TIMER(self
, -1, self
.OnTimerExpire
)
53 EVT_WINDOW_DESTROY(self
, self
.OnQuit
)
56 def SetTickMarkStyle(self
, style
):
58 Set the style of the marks around the edge of the clock.
59 Options are TICKS_NONE, TICKS_SQUARE, and TICKS_CIRCLE
61 self
.tickMarkStyle
= style
64 def SetTickMarkColours(self
, brushC
, penC
="BLACK"):
66 Set the brush colour and optionally the pen colour of
67 the marks around the edge of the clock.
69 self
.tickMarksBrushC
= brushC
70 self
.tickMarksPenC
= penC
72 SetTickMarkColour
= SetTickMarkColours
75 def SetHandsColour(self
, c
):
76 """An alias for SetForegroundColour"""
77 self
.SetForegroundColour(c
) # the hands just use the foreground colour
81 # Using the current settings, render the points and line endings for the
82 # circle inside the specified device context. In this case, the DC is
83 # a memory based device context that will be blitted to the actual
84 # display DC inside the OnPaint() event handler.
85 def OnSize(self
, event
):
86 # The faceBitmap init is done here, to make sure the buffer is always
87 # the same size as the Window
88 size
= self
.GetClientSize()
89 self
.faceBitmap
= wxEmptyBitmap(size
.width
, size
.height
)
93 def OnPaint(self
, event
):
94 self
.DrawHands(wxPaintDC(self
))
97 def OnQuit(self
, event
):
102 def OnTimerExpire(self
, event
):
103 self
.DrawHands(wxClientDC(self
))
106 def DrawHands(self
, drawDC
):
107 # Start by drawing the face bitmap
108 drawDC
.DrawBitmap(self
.faceBitmap
, (0,0))
110 currentTime
= time
.localtime(time
.time())
111 hour
, minutes
, seconds
= currentTime
[3:6]
113 W
,H
= self
.faceBitmap
.GetWidth(), self
.faceBitmap
.GetHeight()
117 radius
= min(centerX
, centerY
)
118 hour
+= minutes
/ 60.0 # added so the hour hand moves continuously
119 x
, y
= self
.point(hour
, 12, (radius
* .65))
120 hourX
, hourY
= (x
+ centerX
), (centerY
- y
)
121 x
, y
= self
.point(minutes
, 60, (radius
* .85))
122 minutesX
, minutesY
= (x
+ centerX
), (centerY
- y
)
123 x
, y
= self
.point(seconds
, 60, (radius
* .85))
124 secondsX
, secondsY
= (x
+ centerX
), (centerY
- y
)
126 # Draw the hour hand...
127 drawDC
.SetPen(wxPen(self
.GetForegroundColour(), 5, wxSOLID
))
128 drawDC
.DrawLine((centerX
, centerY
), (hourX
, hourY
))
130 # Draw the minutes hand...
131 drawDC
.SetPen(wxPen(self
.GetForegroundColour(), 3, wxSOLID
))
132 drawDC
.DrawLine((centerX
, centerY
), (minutesX
, minutesY
))
134 # Draw the seconds hand...
135 drawDC
.SetPen(wxPen(self
.GetForegroundColour(), 1, wxSOLID
))
136 drawDC
.DrawLine((centerX
, centerY
), (secondsX
, secondsY
))
139 # Draw the specified set of line marks inside the clock face for the
140 # hours or minutes...
142 backgroundBrush
= wxBrush(self
.GetBackgroundColour(), wxSOLID
)
143 drawDC
= wxMemoryDC()
144 drawDC
.SelectObject(self
.faceBitmap
)
145 drawDC
.SetBackground(backgroundBrush
)
148 W
,H
= self
.faceBitmap
.GetWidth(), self
.faceBitmap
.GetHeight()
152 # Draw the marks for hours and minutes...
153 self
.DrawTimeMarks(drawDC
, self
.minuteMarks
, centerX
, centerY
, 4)
154 self
.DrawTimeMarks(drawDC
, self
.hourMarks
, centerX
, centerY
, 9)
157 def DrawTimeMarks(self
, drawDC
, markCount
, centerX
, centerY
, markSize
):
158 for i
in range(markCount
):
159 x
, y
= self
.point(i
+ 1, markCount
, min(centerX
,centerY
) - 16)
160 scaledX
= x
+ centerX
- markSize
/2
161 scaledY
= centerY
- y
- markSize
/2
163 drawDC
.SetBrush(wxBrush(self
.tickMarksBrushC
, wxSOLID
))
164 drawDC
.SetPen(wxPen(self
.tickMarksPenC
, 1, wxSOLID
))
165 if self
.tickMarkStyle
!= self
.TICKS_NONE
:
166 if self
.tickMarkStyle
== self
.TICKS_CIRCLE
:
167 drawDC
.DrawEllipse((scaledX
- 2, scaledY
), (markSize
, markSize
))
169 drawDC
.DrawRectangle((scaledX
- 3, scaledY
), (markSize
, markSize
))
172 def point(self
, tick
, range, radius
):
173 angle
= tick
* (360.0 / range)
174 radiansPerDegree
= math
.pi
/ 180
175 pointX
= int(round(radius
* math
.sin(angle
* radiansPerDegree
)))
176 pointY
= int(round(radius
* math
.cos(angle
* radiansPerDegree
)))
177 return wxPoint(pointX
, pointY
)
182 if __name__
== "__main__":
185 frame
= wxFrame(None, -1, "AnalogClockWindow Test", size
=(375,375))
187 clock
= AnalogClockWindow(frame
)
188 clock
.SetTickMarkColours("RED")
189 clock
.SetHandsColour("WHITE")
190 clock
.SetBackgroundColour("BLUE")
194 self
.SetTopWindow(frame
)