Python Forum
Playing around with a clock's seconds in tkinter - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: General (https://python-forum.io/forum-1.html)
+--- Forum: Code sharing (https://python-forum.io/forum-5.html)
+--- Thread: Playing around with a clock's seconds in tkinter (/thread-36363.html)



Playing around with a clock's seconds in tkinter - menator01 - Feb-11-2022

Has a grid of numbers from 00 - 59 that highlights the background matching the clocks seconds.

import tkinter as tk
from time import strftime

class Window(tk.Frame):
    def __init__(self, parent):
        parent.columnconfigure(0, weight=1)
        parent.columnconfigure(1, weight=3)

        text_label = tk.Label(parent, text='Time:')
        text_label['relief'] = 'groove'
        text_label.grid(column=0, row=0, sticky='new', padx=2)

        self.clock = tk.Label(parent)
        self.clock['relief'] = 'groove'
        self.clock.grid(column=1, row=0, sticky='new', padx=2)

        container = tk.Frame()
        container.grid(column=0, row=1, columnspan=2, sticky='new')
        for i in range(4):
            container.grid_columnconfigure(i, weight=3, uniform='images')

        self.label = []
        self.label_text = [i for i in range(60)]
        col = 0
        row = 0
        for i in range(60):
            self.label.append(tk.Label(container, width=3, bg='red', height=2))
            self.label[i]['relief'] = 'raised'
            self.label[i]['borderwidth'] = 1
            self.label[i]['text'] = f'{self.label_text[i]:02d}'
            self.label[i].grid(column=col, row=row, sticky='new', padx=2, pady=2)
            if col > 4:
                row += 1
                col = 0
            else:
                col += 1

        self.update()

    def update(self):
        self.clock['text'] = strftime('%I:%M:%S %p')
        for i in range(60):
            if self.clock['text'][6:8] in self.label[i]['text']:
                self.label[i]['bg'] = 'gold'
                self.label[i]['fg'] = 'black'
            else:
                self.label[i]['bg'] = 'black'
                self.label[i]['fg'] = 'white'

        self.clock.after(1000, self.update)

def main():
    root = tk.Tk()
    root['padx'] = 8
    root['pady'] = 4
    Window(root)
    root.mainloop()

if __name__ == '__main__':
    main()



RE: Playing around with a clock's seconds in tkinter - menator01 - Feb-12-2022

I did the whole clock

import tkinter as tk
from time import strftime

class Numbers:
    def __init__(self, parent, main_row, start, end):

        frame = tk.Frame(parent)
        frame.grid(column=0, row=main_row, sticky='news')
        for i in range(6):
            frame.grid_columnconfigure(i, weight=3, uniform='frames')

        self.labels = []
        col = 0
        row = 0

        for i in range(start, end):
            self.labels.append(tk.Label(frame, text=f'{i:02d}'))
            self.labels[i]['relief'] = 'ridge'
            self.labels[i]['font'] = ('tahoma 14 normal')
            self.labels[i].grid(column=col, row=row, sticky='news', padx=2, pady=2)
            if col > 10:
                col = 0
                row += 1
            else:
                col += 1

class Window:
    def __init__(self, parent):
        self.parent = parent
        frame = tk.Frame(parent)
        frame.grid(column=0, row=0, sticky='news')

        hrs = tk.LabelFrame(frame, text='Hours', font=('verdana 14 bold'))
        hrs.grid(column=0, row=0, sticky='news')

        mins = tk.LabelFrame(frame, text='Minutes', font=('verdana 14 bold'))
        mins.grid(column=0, row=1, sticky='news', pady=8)

        sec = tk.LabelFrame(frame, text='Seconds', font=('verdana 14 bold'))
        sec.grid(column=0, row=2, sticky='news')

        self.hours = Numbers(hrs, 0, 0, 24)
        self.minutes = Numbers(mins, 0, 0, 60)
        self.seconds = Numbers(sec, 0, 0, 60)

        self.update()

    def update(self):
        clock = strftime('%H:%M:%S')
        for i in range(len(self.seconds.labels)):
            if self.seconds.labels[i]['text'] == clock[6:8]:
                self.seconds.labels[i]['bg'] = 'gold'
                self.seconds.labels[i]['fg'] = 'brown'
            else:
                self.seconds.labels[i]['bg'] = 'lightgray'
                self.seconds.labels[i]['fg'] = 'black'


        for i in range(len(self.minutes.labels)):
            if self.minutes.labels[i]['text'] == clock[3:5]:
                self.minutes.labels[i]['bg'] = 'gold'
                self.minutes.labels[i]['fg'] = 'brown'
            else:
                self.minutes.labels[i]['bg'] = 'lightgray'
                self.minutes.labels[i]['fg'] = 'black'

        for i in range(len(self.hours.labels)):
            if self.hours.labels[i]['text'] == clock[0:2]:
                self.hours.labels[i]['bg'] = 'gold'
                self.hours.labels[i]['fg'] = 'brown'
            else:
                self.hours.labels[i]['bg'] = 'lightgray'
                self.hours.labels[i]['fg'] = 'black'

        self.parent.after(1000, self.update)

def main():
    root = tk.Tk()
    root['padx'] = 5
    root['pady'] = 3
    Window(root)
    root.mainloop()

main()