Last active
April 6, 2023 21:23
-
-
Save olisolomons/b7b924628881a044638e68adb6982fd1 to your computer and use it in GitHub Desktop.
Simple GTK clock
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/python3 | |
| # This code is licensed under the terms of the GPL3 license | |
| # Written by: Oli Solomons | |
| # 2023 | |
| import gi | |
| gi.require_version("Gtk", "3.0") | |
| from gi.repository import Gtk, Gdk, GLib | |
| import threading | |
| from datetime import * | |
| class MyWindow(Gtk.Window): | |
| def __init__(self): | |
| super().__init__(title="Clock") | |
| self.set_decorated(False) | |
| self.fullscreen() | |
| self.connect("key-press-event", self.on_key_press_event) | |
| provider = Gtk.CssProvider() | |
| screen = Gdk.Screen.get_default() | |
| provider = Gtk.CssProvider() | |
| style_context = Gtk.StyleContext() | |
| style_context.add_provider_for_screen( | |
| screen, provider, Gtk.STYLE_PROVIDER_PRIORITY_APPLICATION | |
| ) | |
| css = b""" | |
| #clock_label { | |
| background: #383b40; | |
| font-family: Ubuntu; | |
| font-size: 40pt; | |
| color: #d3d3d3; | |
| border-radius: 5px; | |
| padding: 15px; | |
| box-shadow: 0 0 10px black; | |
| } | |
| """ | |
| provider.load_from_data(css) | |
| self.grid = Gtk.Grid() | |
| self.add(self.grid) | |
| def expand(x, y): | |
| l = Gtk.Label(label=' ') | |
| l.set_hexpand(True) | |
| l.set_vexpand(True) | |
| self.grid.attach(l, x, y, 1, 1) | |
| expand(0, 0) | |
| self.clock = Gtk.Label() | |
| self.clock.set_name('clock_label') | |
| self.update_time() | |
| self.grid.attach(self.clock, 1, 1, 1, 1) | |
| expand(2, 2) | |
| # Initialize colors, alpha transparency | |
| self.set_app_paintable(1) | |
| self.gtk_screen = self.get_screen() | |
| if not self.gtk_screen.is_composited(): | |
| self.supports_alpha = False | |
| else: | |
| self.supports_alpha = True | |
| self.alive = threading.RLock() | |
| self.alive.acquire() | |
| self.t = threading.Thread(target=self.time_loop) | |
| self.t.start() | |
| def expose(self, widget, event): | |
| pass | |
| def on_key_press_event(self, widget, event): | |
| if event.get_keyval()[1] in {ord('q'), ord('c'), Gdk.KEY_Escape}: | |
| self.alive.release() | |
| def time_loop(self): | |
| while not self.alive.acquire(timeout=1): | |
| GLib.idle_add(self.update_time) | |
| GLib.idle_add(Gtk.main_quit) | |
| def update_time(self): | |
| t = datetime.now() | |
| self.clock.set_text(t.strftime('%H:%M:%S')) | |
| def on_destroy(self): | |
| self.alive.release() | |
| win = MyWindow() | |
| win.show_all() | |
| Gtk.main() |
Author
Again thank you very much. You can find my project over here: OpenRadio and i recommend using GLib.timeout_add instead of an thread.
Author
Interesting project! I didn't know about GLib.timeout_add, thanks @1337Misom
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@1337Misom Yes, please go ahead :)
I've added a license line to the gist, hopefully I did it right! (The code was written by me, not copied from elsewhere.)