Skip to content

Instantly share code, notes, and snippets.

@jampola
Last active April 3, 2023 08:56
Show Gist options
  • Save jampola/473e963cff3d4ae96707 to your computer and use it in GitHub Desktop.
Save jampola/473e963cff3d4ae96707 to your computer and use it in GitHub Desktop.
Simple clock using PyGTK and GObject.timeout_add()
#!/usr/bin/python
from gi.repository import Gtk, GObject
from datetime import datetime
class MainWindow(Gtk.Window):
def __init__(self):
Gtk.Window.__init__(self, title="app")
self.box = Gtk.Box(spacing=6)
self.add(self.box)
self.label = Gtk.Label()
self.box.pack_start(self.label, True, True, 0)
# Displays Timer
def displayclock(self):
# putting our datetime into a var and setting our label to the result.
# we need to return "True" to ensure the timer continues to run, otherwise it will only run once.
datetimenow = str(datetime.now())
self.label.set_label(datetimenow)
return True
# Initialize Timer
def startclocktimer(self):
# this takes 2 args: (how often to update in millisec, the method to run)
GObject.timeout_add(1000, self.displayclock)
win = MainWindow()
win.connect("delete-event", Gtk.main_quit)
win.show_all()
win.startclocktimer()
Gtk.main()
@MJtuks
Copy link

MJtuks commented Sep 29, 2020

Thank you for this

@kontrarie
Copy link

Many thanks, this got me out of an issue that had been haunting me for a long while.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment