Python clock - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: GUI (https://python-forum.io/forum-10.html) +--- 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 = 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() 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() 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() RE: Python clock - menator01 - May-14-2020 Thank you. |