Python Forum
Python clock - Printable Version

+- Python Forum (
+-- Forum: Python Coding (
+--- Forum: GUI (
+--- Thread: Python clock (/thread-26813.html)

Python clock - menator01 - May-14-2020

I'm trying my luck with a clock
Just can't seem to get it to update.
I've tried with the both the after method and while loops.
It would appear that I'm not putting it in the right place in the code or I need to make a new function and call it.
Any help would be great. Thanks

# /usr/bin/env python3

import os
import tkinter as tk
from tkinter import ttk
from datetime import datetime

class AlarmClock:
    def __init__(self, master):
        self.master = master
        self.master.columnconfigure(0, weight=1)
        self.master.rowconfigure(0, weight=1)

        style = ttk.Style()
        style.configure('Clock.TFrame', background='slategray')
        self.clock_frame = ttk.Frame(self.master, style='Clock.TFrame', border=8)
        self.clock_frame.grid(column=0, row=0, sticky='new')
        self.clock_frame.grid_columnconfigure(0, weight=3)

        now =
        time_string = now.strftime('%H:%M:%S')
        self.show_time = tk.StringVar()

    def clock(self):

        clock_nums = ttk.Style()
        clock_nums.configure('Clock.TLabel', background='black', foreground='red', anchor='n', \
                             font=('sans', 18, 'bold'), relief='ridge', padding=8)
        self.label = ttk.Label(self.clock_frame, text=self.show_time.get(), style='Clock.TLabel')
        self.label.grid(column=0, row=0, sticky='new')
        self.label.grid_columnconfigure(0, weight=3)

def main():
    root = tk.Tk()
    root.title('Alarm Clock')

if __name__ == '__main__':

RE: Python clock - Larz60+ - May-14-2020

Here's a simple clock that uses after command:
import tkinter as tk
from time import strftime

class Clock:
    def __init__(self, parent=None):
        # following allows iportiing class into another GUI framework.
        if not parent:
            self.parent = tk.Tk()
            self.parent = parent

        self.parent.title('My Clock')

        self.clock = tk.Label(self.parent, borderwidth = 2, height=2, relief=tk.SOLID)
        self.clock.pack(padx=5, pady=2, fill=tk.BOTH)
        self.clock['text'] = strftime("%H:%M:%S")

        self.time_now = strftime("%H:%M:%S")

        if not parent:

    def display_time(self):
        self.clock['text'] = strftime("%H:%M:%S")
    def increnment_time(self):
        # call self every 1000 ms (1 sec)
        self.clock.after(1000, self.increnment_time)

if __name__ == '__main__':
    clk = Clock()

RE: Python clock - menator01 - May-14-2020

Thank you.