-2

I am trying to get the current time to show and update every second in a tkinter label.

Im assuming that i am doing this completely wrong. Im rather new to this and trying to work through it bit by bit.

from tkinter import *
import sys
import time

#function to close window when ESC is pressed
def close(event):
    sys.exit()

def curtime():
    while 1==1:
        current_time = time.strftime('%H:%M:%S')
        print(current_time)
        time.sleep(1)

#Main System
window = Tk()
window.state('zoomed')
window.configure(bg='#3a3a3a')

#Frames
top_frame = Frame(window)
top_frame.pack(side=TOP)
bottom_frame = Frame(window)
bottom_frame.pack(side=BOTTOM)

#Labels
header_label = Label(top_frame, text="SARA", background='#3a3a3a', fg='#ffffff', font=("calibri", 50))
header_label.pack(side=LEFT)



header_label2 = Label(top_frame, textvariable = curtime(), background='#3a3a3a', fg='#ffffff', font=("calibri", 50))
header_label2.pack(side=LEFT)




#on press ESC window closes
window.bind('<Escape>', close)
window.mainloop()
Rogersj
  • 31
  • 6
  • Have you done any research? There are many questions on this site about displaying clocks and timers with tkinter. – Bryan Oakley Apr 10 '20 at 20:47
  • yes i have read a bunch of them just cant seem to find something that shows the outcome i want to get to. I use posting as a last resort – Rogersj Apr 10 '20 at 20:50
  • Read up on [python-tkinter-label-redrawing-every-10-seconds](https://stackoverflow.com/questions/17847869) – stovfl Apr 10 '20 at 22:22
  • [this](https://stackoverflow.com/questions/15689667/digital-clock-in-status-bar-in-python-3-and-tkinter?r=SearchResults) is one similar question. – acw1668 Apr 11 '20 at 00:47

1 Answers1

2
#function to get timer to calculate
def timer():
    timer_tick = strftime("%I:%M:%S")
    time_label.configure(text=timer_tick)
    time_label.after(1000, timer)

time_label = Label(top_frame, background='#3a3a3a', fg='#ffffff', font=("calibri", 50))
time_label.pack(side=LEFT)




#if statement which constantly returns true to make the timer refresh and tick
if __name__ == "__main__":
    timer()
Rogersj
  • 31
  • 6