Python Forum
tkinter 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: tkinter clock (/thread-18922.html)



tkinter clock - Ondrej - Jun-06-2019

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()



RE: tkinter clock - heiner55 - Jun-06-2019

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()



RE: tkinter clock - Ondrej - Jun-06-2019

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!!!


RE: tkinter clock - heiner55 - Jun-06-2019

if time_string == "14:20:30":
        clock["bg"] = ...



RE: tkinter clock - Ondrej - Jun-06-2019

Thank you!!!!!


RE: tkinter clock - heiner55 - Jun-06-2019

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)