根据时间变化更改文本颜色

时间:2020-11-03 09:04:34

标签: python python-3.x tkinter

我编写了一些计时器,并根据我在Internet上找到的代码用tkinter可视化了这些计时器。这对我来说仍然很好,但是现在我需要添加一个功能。如果全局计时器变量(timedelta_amp等)小于3分钟,我想更改计时器的字体颜色。由于无法百分百理解复制的代码,因此无法添加此功能。

# importing whole module 
from tkinter import * 
# importing strftime function to 
# retrieve system's time 
from time import strftime
# importing datetime modul
from datetime import datetime, timedelta, time
# close the tkinter windwo with escape
def close_escape(event=None):
    print("escaped")
    root.destroy()
root = Tk()
root.title("Countdown Intraday Handel - Stunden Produkte") 
root.attributes("-fullscreen", False) 
root.bind("<Escape>", close_escape)
root.configure(background='orange')

Text = Label(root, font=('arial', 75, 'bold'), fg='white',bg='orange')
Text.pack()
Frame = Label(root, font=('arial', 20, 'bold'), fg='white',bg='orange')
Frame.pack()

Text1 = Label(root, font=('arial', 50, 'italic'), fg='white',bg='orange')
Text1.pack()
clock_schedulingarea = Label(root, font=('arial',50, 'bold'), fg='white',bg='orange')
clock_schedulingarea.pack()
Frame1 = Label(root, font=('arial', 20, 'bold'), fg='white',bg='orange')
Frame1.pack()

Text2 = Label(root, font=('arial', 50, 'italic'), fg='white',bg='orange')
Text2.pack()
clock_germany = Label(root, font=('arial', 50, 'bold'), fg='white', bg='orange')
clock_germany.pack()
Frame2 = Label(root, font=('arial', 20, 'bold'), fg='white',bg='orange')
Frame2.pack()

Text3 = Label(root, font=('arial', 50, 'italic'), fg='white',bg='orange')
Text3.pack()
clock_sidc = Label(root, font=('arial', 50, 'bold'), fg='white', bg='orange')
clock_sidc.pack()


def tick(): 
    global var1
    global timedelta_amp
    global var2
    global timedelta_de
    global var3
    global timedelta_sidc
    
    ActTimeMinutes = strftime("%M:%S")
    ActTimeHour = strftime("%H")
    
    ActTimeMinutesplus30 = datetime.now() + timedelta(minutes=30)
    ActTimeMinutesplus30 = ActTimeMinutesplus30.strftime("%M:%S")
    
    ActTimeHourplus1 = datetime.now() + timedelta(hours=1)
    ActTimeHourplus1 = ActTimeHourplus1.strftime("%H")
    
    ActTimeHourplus2 = datetime.now() + timedelta(hours=2)
    ActTimeHourplus2 = ActTimeHourplus2.strftime("%H")
    
    ActTimeHourplus3 = datetime.now() + timedelta(hours=3)
    ActTimeHourplus3 = ActTimeHourplus3.strftime("%H")

    quarter_past = "14:59"
    half = "29:59"
    quarter_to = "44:59"
    hour_amp = "54:59"
    hour = "59:59"
    SA = ["AMP"]
    SB = ["DE"]
    SC = ["SIDC"]
    
    if ActTimeMinutes >= "00:00" and ActTimeMinutes < "55:00":
        var1 = [SA, ActTimeHourplus1,"-", ActTimeHourplus2]
        timedelta_amp = datetime.strptime(hour_amp, "%M:%S") - datetime.strptime(ActTimeMinutes, "%M:%S")
    
    elif ActTimeMinutes >= "55:00" and ActTimeMinutes < "59:59":
        var1 = [SA, ActTimeHourplus1,"-", ActTimeHourplus2]
        timedelta_amp = ("Gate Closure")
    
    
    if ActTimeMinutes >= "00:00" and ActTimeMinutes < "30:00" :
        var2 = [SB, ActTimeHourplus1,"-", ActTimeHourplus2]
        timedelta_de = datetime.strptime(half, "%M:%S") - datetime.strptime(ActTimeMinutes, "%M:%S")
        
    elif ActTimeMinutes >= "30:00" and ActTimeMinutes < "59:59":
        var2 = [SB, ActTimeHourplus2,"-", ActTimeHourplus3]
        timedelta_de = datetime.strptime(hour, "%M:%S") - datetime.strptime(ActTimeMinutesplus30, "%M:%S")
            

    var3 = [SC, ActTimeHourplus2,"-", ActTimeHourplus3]
    timedelta_sidc = datetime.strptime(hour, "%M:%S") - datetime.strptime(ActTimeMinutes, "%M:%S")
    
  
    Text.config(text="Stunden Produkte")
    Frame.config(text="______________________________________________________________________________________________")
    
    clock_schedulingarea.config(text=timedelta_amp)
    Text1.config(text=var1)
    Frame1.config(text="___________________________________")
    
    clock_germany.config(text=timedelta_de)
    Text2.config(text=var2)
    Frame2.config(text="___________________________________")
    
    clock_sidc.config(text=timedelta_sidc)
    Text3.config(text=var3)
    
    clock_sidc.after(20, tick)

tick()
root.mainloop()

1 个答案:

答案 0 :(得分:0)

您可以根据tick()中的 timedelta 设置这些计时器的颜色,如下所示:

def tick():
    ...
    if ActTimeMinutes >= "00:00" and ActTimeMinutes < "55:00":
        var1 = [SA, ActTimeHourplus1,"-", ActTimeHourplus2]
        timedelta_amp = datetime.strptime(hour_amp, "%M:%S") - datetime.strptime(ActTimeMinutes, "%M:%S")
        color_amp = 'red' if timedelta_amp.seconds < 180 else 'white' # set color_amp based on timedelta_amp
    elif ActTimeMinutes >= "55:00" and ActTimeMinutes < "59:59":
        var1 = [SA, ActTimeHourplus1,"-", ActTimeHourplus2]
        timedelta_amp = "Gate Closure"
        color_amp = 'red'  # change to other color to suit your case

    if ActTimeMinutes >= "00:00" and ActTimeMinutes < "30:00" :
        var2 = [SB, ActTimeHourplus1,"-", ActTimeHourplus2]
        timedelta_de = datetime.strptime(half, "%M:%S") - datetime.strptime(ActTimeMinutes, "%M:%S")
    elif ActTimeMinutes >= "30:00" and ActTimeMinutes < "59:59":
        var2 = [SB, ActTimeHourplus2,"-", ActTimeHourplus3]
        timedelta_de = datetime.strptime(hour, "%M:%S") - datetime.strptime(ActTimeMinutesplus30, "%M:%S")
    color_de = 'red' if timedelta_de.seconds < 180 else 'white' # set color_de based on timedelta_de
            
    var3 = [SC, ActTimeHourplus2,"-", ActTimeHourplus3]
    timedelta_sidc = datetime.strptime(hour, "%M:%S") - datetime.strptime(ActTimeMinutes, "%M:%S")
    color_sidc = 'red' if timedelta_sidc.seconds < 180 else 'white' # set color_sidc based on timedelta_sidc
    
    Text.config(text="Stunden Produkte")
    Frame.config(text="______________________________________________________________________________________________")
    
    clock_schedulingarea.config(text=timedelta_amp, fg=color_amp) # use color_amp
    Text1.config(text=var1)
    Frame1.config(text="___________________________________")
    
    clock_germany.config(text=timedelta_de, fg=color_de) # use color_de
    Text2.config(text=var2)
    Frame2.config(text="___________________________________")
    
    clock_sidc.config(text=timedelta_sidc, fg=color_sidc) # use color_sidc
    Text3.config(text=var3)
    
    clock_sidc.after(20, tick)