Skip to content

Instantly share code, notes, and snippets.

@critiqjo
Last active August 26, 2016 18:42
Show Gist options
  • Save critiqjo/81c739435193433ac673cddce952ea9f to your computer and use it in GitHub Desktop.
Save critiqjo/81c739435193433ac673cddce952ea9f to your computer and use it in GitHub Desktop.
#!/bin/env python2
import gi
gi.require_version('Gtk', '3.0')
gi.require_version('PangoCairo', '1.0')
from gi.repository import Gtk, GLib, Pango, PangoCairo
from datetime import datetime
class DigitalClock(Gtk.DrawingArea):
def __init__(self):
super(DigitalClock, self).__init__()
self.connect("draw", self.draw)
self._font = Pango.FontDescription("Monospace 25")
GLib.timeout_add(1000, self.update)
def draw(self, widget, context):
rect = self.get_allocation()
context.rectangle(rect.x, rect.y, rect.width, rect.height)
context.set_source_rgb(0.1, 0.1, 0.1)
context.fill()
context.set_source_rgb(0.8, 0.8, 0.8)
self.draw_text(context, rect)
return False
def draw_text(self, context, rect):
text = u"{:%H:%M:%S}".format(datetime.now())
layout = PangoCairo.create_layout(context)
layout.set_font_description(self._font)
layout.set_text(text, len(text))
text_rect = layout.get_extents().logical_rect
x = rect.x + (rect.width - text_rect.width/Pango.SCALE)/2
y = rect.y + (rect.height - text_rect.height/Pango.SCALE)/2
context.move_to(x, y)
PangoCairo.update_layout(context, layout)
PangoCairo.show_layout(context, layout)
def update(self):
self.queue_draw()
return True
def main():
win = Gtk.Window()
clock = DigitalClock()
win.add(clock)
win.connect("delete-event", Gtk.main_quit)
win.show_all()
Gtk.main()
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment