Skip to content

Instantly share code, notes, and snippets.

@TonyDiana
Forked from jampola/clock.py
Created March 2, 2021 17:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save TonyDiana/2da38f10dabf50c190670177543af0b4 to your computer and use it in GitHub Desktop.
Save TonyDiana/2da38f10dabf50c190670177543af0b4 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()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment