Last active
August 29, 2015 14:16
-
-
Save emptypage/3f62a8839f06f5b540b8 to your computer and use it in GitHub Desktop.
A wxPython-based sample clock app.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env python | |
| from __future__ import (absolute_import, | |
| division, | |
| print_function, | |
| unicode_literals) | |
| import math | |
| import time | |
| import wx | |
| class ClockWindow(wx.Window): | |
| def __init__(self, parent, id=-1, | |
| pos=wx.DefaultPosition, size=wx.DefaultSize, | |
| style=0, name=wx.PanelNameStr): | |
| wx.Window.__init__(self, parent, id, pos, size, style, name) | |
| self.SetBackgroundColour(wx.WHITE) | |
| self.lr_clock = 128 | |
| self._bmp = wx.EmptyBitmap(self.lr_clock*2, self.lr_clock*2) | |
| self._timer = wx.Timer(self) | |
| self.Bind(wx.EVT_PAINT, self.OnPaint) | |
| self.Bind(wx.EVT_TIMER, self.OnTimer) | |
| ms_resolution = 1000/30 | |
| self._timer.Start(ms_resolution) | |
| def OnPaint(self, evt): | |
| dc = wx.PaintDC(self) | |
| dw_client, dh_client = self.GetClientSizeTuple() | |
| dc.DrawBitmap(self._bmp, 0, 0) | |
| def OnTimer(self, evt): | |
| wx.CallAfter(self.draw_clock_on_bitmap) | |
| def draw_clock_on_bitmap(self): | |
| lr_clock = self.lr_clock | |
| lx_center = ly_center = lr_clock | |
| lr_bezel = self.lr_clock * 0.95 | |
| lr_shaft = lr_clock / 2 * 0.1 | |
| lr_hour_hand = lr_clock * 0.7 | |
| lr_min_hand = lr_clock * 0.8 | |
| lr_sec_hand = lr_clock * 0.9 | |
| tm = time.localtime() | |
| tm_sec = time.time() % 60 | |
| tm_min = tm.tm_min + tm_sec / 60 | |
| tm_hour = tm.tm_hour + tm_min / 60 | |
| rad_hour = tm_hour / 6 * math.pi - math.pi/2 | |
| rad_min = tm_min / 30 * math.pi - math.pi/2 | |
| rad_sec = tm_sec / 30 * math.pi - math.pi/2 | |
| lx_hour = lx_center + lr_hour_hand * math.cos(rad_hour) | |
| ly_hour = ly_center + lr_hour_hand * math.sin(rad_hour) | |
| ly_min = ly_center + lr_min_hand * math.sin(rad_min) | |
| lx_min = lx_center + lr_min_hand * math.cos(rad_min) | |
| lx_sec = lx_center + lr_sec_hand * math.cos(rad_sec) | |
| ly_sec = ly_center + lr_sec_hand * math.sin(rad_sec) | |
| mdc = wx.MemoryDC(self._bmp) | |
| mdc.Clear() | |
| gc = wx.GraphicsContext.Create(mdc) | |
| gc.SetFont(self.GetFont()) | |
| label = '%02d:%02d:%02d' % (tm_hour, tm_min, tm_sec) | |
| lw_label, lh_label = gc.GetTextExtent(label) | |
| lx_label = lx_center - lw_label / 2 | |
| ly_label = lr_clock * 1.7 | |
| gc.DrawText(label, lx_label, ly_label) | |
| gc.SetPen(wx.BLACK_PEN) | |
| gc.SetBrush(wx.TRANSPARENT_BRUSH) | |
| gc.DrawEllipse(lr_clock-lr_bezel, lr_clock-lr_bezel, | |
| lr_bezel*2, lr_bezel*2) | |
| gc.DrawLines(((lx_center, ly_center), (lx_hour, ly_hour))) | |
| gc.DrawLines(((lx_center, ly_center), (lx_min, ly_min))) | |
| gc.SetPen(wx.RED_PEN) | |
| gc.DrawLines(((lx_center, ly_center), (lx_sec, ly_sec))) | |
| gc.SetPen(wx.BLACK_PEN) | |
| gc.SetBrush(wx.BLACK_BRUSH) | |
| gc.DrawEllipse(lx_center-lr_shaft, ly_center-lr_shaft, | |
| lr_shaft*2, lr_shaft*2) | |
| self.Refresh() | |
| mdc.SelectObject(wx.NullBitmap) | |
| def main(): | |
| app = wx.App() | |
| frame = wx.Frame(None) | |
| child = ClockWindow(frame) | |
| frame.Show() | |
| app.MainLoop() | |
| if __name__ == '__main__': | |
| main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment