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
#2
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()
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
RE: Playing around with a clock's seconds in tkinter - by menator01 - Feb-12-2022, 04:32 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Simple wxpython digital clock menator01 2 4,153 Feb-20-2021, 05:47 PM
Last Post: menator01
  My Attempt at an alarm clock menator01 0 2,498 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