Python Forum

Full Version: tkinter clock
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hello everyone,

I need help to make simple clock app which will change background color in choosen periods with duration for few secc and after then transform back. I created simple clock with tkinter, which you can see bellow, but I have problem with conditions of the changing of background color. I will appreciate any idea. thanks

import sys
from tkinter import *
import time

def tick():
    time_string=time.strftime("%H:%M:%S")
    clock.config(text=time_string)
    clock.after(200,tick)
    
root=Tk()
clock=Label(root, font=("times", 100, "bold"), bg="red")
clock.grid(row=0, column=1)
tick()
root.mainloop()
import sys, random
from tkinter import *
import time
.
colors = ( "red", "black", "blue" )

def tick():
    time_string=time.strftime("%H:%M:%S")
    clock.config(text=time_string)
    clock.after(1000,tick)
    clock["bg"] = colors[random.randint(0, len(colors)-1)]

root=Tk()
clock=Label(root, font=("times", 100, "bold"), bg="red")
clock.grid(row=0, column=1)
tick()
root.mainloop()
Thank you very much!!! Just one more thing and I will be absolutely satisfied. Is possible to set exactly time when should clock change the color? For example 14:20:30 change color to red with duration 40 secc and repeat again after 15 min... Thank you very much!!!
if time_string == "14:20:30":
        clock["bg"] = ...
Thank you!!!!!
15 min later:

#!/usr/bin/python3
import time

# time now
time_sec = time.time()
time_string = time.strftime("%H:%M:%S", time.localtime(time_sec))
print(time_string)

# time 15 min later
time_sec = time_sec + 15*60
time_string = time.strftime("%H:%M:%S", time.localtime(time_sec))
print(time_string)