#!/usr/bin/python3
#
# Hex-Clock nach http://www.kraftfuttermischwerk.de/blogg/die-uhrzeit-als-hexadezimale-farbdefinition/
#
# Copyright (C) 2015 Robert Bienert, http://www.robertbienert.de/impressum/
#
# "THE BEER-WARE LICENSE" (Revision 42):
# <robertbienert@gmx.net> wrote this file. As long as you retain this notice you
# can do whatever you want with this stuff. If we meet some day, and you think
# this stuff is worth it, you can buy me a beer in return Robert Bienert

import tkinter as tk
import time
import threading

class Timer(threading.Thread):
    """
    See https://docs.python.org/3/library/threading.html for
    differences between this class and threading.Timer:
    This class runs periodically every dt seconds until the
    program is terminated or a RuntimeError occurs.
    threading.Timer runs only once.
    """
    def __init__(self, dt,method):
        """
        initializes the Timer object with the time interval dt
        and a handle to the method to run().
        """
        threading.Thread.__init__(self)
        self.waiting = dt
        self.execute = method

    def run(self):
        while True:
            try:
                time.sleep(self.waiting)
                self.execute()
            except RuntimeError:
                return    # cancels thread execution

class App(tk.Frame):
    """
    see https://docs.python.org/3/library/tkinter.html
    """
    def __init__(self, master=None):
        tk.Frame.__init__(self, master)

        self.createWidgets()

        self.pack()

 # For how to change the window size see the bottom of this
 # source file.

        self.timer = Timer(1.0, self.updateWidgets)
        self.timer.start()

    def createWidgets(self):
        """
        see http://infohost.nmt.edu/tcc/help/pubs/tkinter/web/control-variables.html
        for details about using so-called control variables:
        They let you auto-update a widget text if the variable
        gets updated.
        """
        self.timeVar = tk.StringVar()
        self.timeVar.set("hh:mm:ss")    # initial
        self.time = tk.Label(self, textvariable=self.timeVar)
        self.time.pack()

        self.hexTVar = tk.StringVar()
        self.hexTVar.set("#rrggbb")    # initial
        self.hext = tk.Label(self, textvariable=self.hexTVar)
        self.hext.pack()

    def updateWidgets(self):
        """
        This method is called periodically by our timer.
        See https://docs.python.org/3/library/time.html#time.strftime
        for details about formatting time "bits".
        """
        self.timeVal = time.gmtime()
        self.timeVar.set(time.strftime('%H:%M:%S', self.timeVal))
        hextime = time.strftime('#%H%M%S', self.timeVal)
        self.hexTVar.set(hextime)

        # window background, self is "only" a tkinter.Frame
        self.master['bg'] = hextime

wnd = tk.Tk()
app = App(wnd)

# changing the window size (i.e. geometry), see
# http://www.suncol.de/programmierung/python/70-joerg.html
# https://www.daniweb.com/software-development/python/threads/322818/tkinter-window-size
wnd.geometry('300x200')

app.mainloop()