Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Python clock
#1
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 = datetime.now()
        time_string = now.strftime('%H:%M:%S')
        self.show_time = tk.StringVar()
        self.show_time.set(time_string)
        self.clock()

    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')
    root.geometry('400x200+50+50')
    AlarmClock(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
#2
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()
        else:
            self.parent = parent

        self.parent.title('My Clock')
        self.parent.geometry('200x50+10+10')

        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")
        self.increnment_time()

        if not parent:
            self.parent.mainloop()


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

if __name__ == '__main__':
    clk = Clock()
Reply
#3
Thank you.
I welcome all feedback.
The only dumb question, is one that doesn't get asked.
My Github
How to post code using bbtags


Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  [Tkinter] Alarm Clock GUI tickandatock_ 1 4,066 Nov-10-2019, 02:52 AM
Last Post: Larz60+
  tkinter clock Ondrej 5 4,312 Jun-06-2019, 02:09 PM
Last Post: heiner55
  Clock freezes - wx.python glib problem strongheart 3 3,983 Oct-10-2017, 03:36 AM
Last Post: strongheart

Forum Jump:

User Panel Messages

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