Python Forum
Playing around with a clock's seconds in tkinter
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Playing around with a clock's seconds in tkinter
#1
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()
I welcome all feedback.
The only dumb question, is one that doesn't get asked.
My Github
How to post code using bbtags


Reply


Messages In This Thread
Playing around with a clock's seconds in tkinter - by menator01 - Feb-11-2022, 10:25 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Simple wxpython digital clock menator01 2 4,155 Feb-20-2021, 05:47 PM
Last Post: menator01
  My Attempt at an alarm clock menator01 0 2,500 May-15-2020, 06:28 PM
Last Post: menator01
  Binary Clock sparkz_alot 13 14,232 Jan-02-2017, 03:52 PM
Last Post: sparkz_alot

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020