Clock does not run
Question:
I would like to programm a simple clock with python and tkinter indicating local and utc time in the same window. As you can see on the screenshoot, first clock local time is not running, but the second clock below utc time is running well. At the moment I can not figure out the error. Maybe someone of you can help me.
Thanx a lot!
from tkinter import *
import datetime
from time import strftime
root = Tk()
root.geometry("500x500")
root.resizable(0,0)
root.title('Python Clock')
def localtime():
now = datetime.datetime.now()
string = now.strftime('%H:%M:%S')
mark.config(text = string)
mark.after(1000,localtime)
mark = Label(root,
font = ('calibri', 40, 'bold'),
pady=150,
foreground = 'blue')
mark.pack(anchor = 'center')
localtime()
def time_utc():
now = datetime.datetime.utcnow()
string = now.strftime('%H:%M:%S')
mark.config(text = string)
mark.after(1000, time_utc)
mark = Label(root,
font = ('calibri', 40, 'bold'),
pady=150,
foreground = 'blue')
mark.pack(anchor = 's')
time_utc()
mainloop()
Answers:
The problem is that you are trying to create the mark
labels inside the functions. Instead you need to create them once at the top level, and then just call config
and after
inside each function to modify them on each loop:
def localtime():
now = datetime.datetime.now()
string = now.strftime('%H:%M:%S')
locallbl.config(text = string)
locallbl.after(1000,localtime)
def time_utc():
now = datetime.datetime.utcnow()
string = now.strftime('%H:%M:%S')
utclbl.config(text = string)
utclbl.after(1000, time_utc)
utclbl = Label(root,
font = ('calibri', 40, 'bold'),
pady=150,
foreground = 'blue')
locallbl = Label(root,
font = ('calibri', 40, 'bold'),
pady=150,
foreground = 'blue')
locallbl.pack(anchor = 'center')
utclbl.pack(anchor = 's')
localtime()
time_utc()
mainloop()
I would like to programm a simple clock with python and tkinter indicating local and utc time in the same window. As you can see on the screenshoot, first clock local time is not running, but the second clock below utc time is running well. At the moment I can not figure out the error. Maybe someone of you can help me.
Thanx a lot!
from tkinter import *
import datetime
from time import strftime
root = Tk()
root.geometry("500x500")
root.resizable(0,0)
root.title('Python Clock')
def localtime():
now = datetime.datetime.now()
string = now.strftime('%H:%M:%S')
mark.config(text = string)
mark.after(1000,localtime)
mark = Label(root,
font = ('calibri', 40, 'bold'),
pady=150,
foreground = 'blue')
mark.pack(anchor = 'center')
localtime()
def time_utc():
now = datetime.datetime.utcnow()
string = now.strftime('%H:%M:%S')
mark.config(text = string)
mark.after(1000, time_utc)
mark = Label(root,
font = ('calibri', 40, 'bold'),
pady=150,
foreground = 'blue')
mark.pack(anchor = 's')
time_utc()
mainloop()
The problem is that you are trying to create the mark
labels inside the functions. Instead you need to create them once at the top level, and then just call config
and after
inside each function to modify them on each loop:
def localtime():
now = datetime.datetime.now()
string = now.strftime('%H:%M:%S')
locallbl.config(text = string)
locallbl.after(1000,localtime)
def time_utc():
now = datetime.datetime.utcnow()
string = now.strftime('%H:%M:%S')
utclbl.config(text = string)
utclbl.after(1000, time_utc)
utclbl = Label(root,
font = ('calibri', 40, 'bold'),
pady=150,
foreground = 'blue')
locallbl = Label(root,
font = ('calibri', 40, 'bold'),
pady=150,
foreground = 'blue')
locallbl.pack(anchor = 'center')
utclbl.pack(anchor = 's')
localtime()
time_utc()
mainloop()