having problems adding a timer to my home page of my computer science nea project. im wanting the timer to display on the bottom left of the tkinter window home_page and cant seem to figure it out please could you advise me on how to fix this and what I've done wrong. thank you Matthew. code: ''' having problems adding a timer to my home page of my computer science nea project. im wanting the timer to display on the bottom left of the tkinter window home_page and cant seem to figure it out please could you advise me on how to fix this and what I've done wrong. thank you Matthew. code: '''
CodePudding user response:
here is example code for current time and date because you are missing code and I dont know what do you need exactly
example code:
import tkinter as tk
from tkinter import Label, BOTTOM, NW
from datetime import datetime
now = datetime.now()
current_time = now.strftime("%H:%M")
day = datetime.today()
today = day.strftime("%d.%m.%Y")
window = tk.Tk()
window.geometry("400x400")
window.title('TEST')
title_label = Label(window, text="test window with time and date", font=('Courier', 10), fg='Red')
title_label.pack(pady=30)
time_label = Label(window, text=current_time, font=('Courier', 10))
time_label.pack(side=BOTTOM, anchor=NW)
date_label = Label(window, text=today, font=('Courier', 10))
date_label.pack(pady=30)
info_label = Label(window, text="time and date labels are visible", font=('Courier', 8))
info_label.pack(pady=30)
window.mainloop()
CodePudding user response:
This problem was few times on Stackoverflow - I don't remember links but at least I keep my answer on GitHub.
It uses root.after()
to run function after 1 second which update time in Label
and uses again root.after()
to update again after 1 second.
I will not try to put it in bottom left of home_page - it is job for you.
import tkinter as tk
import time
# --- functions ---
def update_time():
label['text'] = time.strftime('%Y.%m.%d %H:%M:%S')
# run update_time again after 1000ms (1s)
root.after(1000, update_time)
# --- main ---
root = tk.Tk()
label = tk.Label(root)
label.pack()
# run update_time first time
update_time() # at once
#root.after(1000, update_time) # after 1 second
root.mainloop()
BTW: My GitHub python-examples and other examples with timer