Skip to content

Commit

Permalink
Added first X11 example (clock)
Browse files Browse the repository at this point in the history
  • Loading branch information
P403n1x87 committed Jun 1, 2018
1 parent 4234f67 commit 7952bb4
Showing 1 changed file with 74 additions and 0 deletions.
74 changes: 74 additions & 0 deletions examples/clock.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
"""
This file is part of "blighty" which is released under GPL.
See file LICENCE or go to http://www.gnu.org/licenses/ for full license
details.
blighty is a desktop widget creation and management library for Python 3.
Copyright (c) 2018 Gabriele N. Tornetta <[email protected]>.
All rights reserved.
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
"""

from blighty import CanvasGravity
from blighty.x11 import Canvas, start_event_loop

import datetime

from math import pi as PI


class Clock(Canvas):
def on_button_pressed(self, button, state, x, y):
self.dispose()

@staticmethod
def hand(ctx, angle, length, thickness):
ctx.save()
ctx.set_source_rgba(1, 1, 1, 1)
ctx.set_line_width(thickness)
ctx.rotate(angle)
ctx.move_to(0, length * .2)
ctx.line_to(0, -length)
ctx.stroke()
ctx.restore()

def on_draw(self, ctx):
now = datetime.datetime.now()

ctx.translate(self.width >> 1, self.height >> 1)

Clock.hand(ctx,
angle = now.second / 30 * PI,
length = (self.height >> 1) * .9,
thickness = 1)

mins = now.minute + now.second / 60
Clock.hand(ctx,
angle = mins / 30 * PI,
length = (self.height >> 1) * .8,
thickness = 3)

hours = (now.hour % 12) + mins / 60
Clock.hand(ctx,
angle = hours / 6 * PI,
length = (self.height >> 1) * .5,
thickness = 6)


if __name__ == "__main__":
clock = Clock(0, 0, 400, 400, gravity = CanvasGravity.CENTER)
clock.show()
start_event_loop()

0 comments on commit 7952bb4

Please sign in to comment.