[英]Change the text color depending on timedelta
我編寫了一些計時器,並根據我在 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()
您可以根據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)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.