【PY】倒计时日历

大家有时候会不会觉得时间记不住呢?PY倒计时日历可以满足你。

main.py:

from tkinter import Tk,Canvas
from datetime import date,datetime
def get_events():
    list_events=[]
    with open('events.txt')as file:
        for line in file:
            line=line.rstrip('\n')
            current_event=line.split(',')
            event_date=datetime.strptime(current_event[1],'%d/%m/%y').date()
            current_event[1]=event_date
            list_events.append(current_event)
    return list_events
def days_between_dates(date1,date2):
    time_between=str(date1-date2)
    number_of_days=time_between.split(' ')
    return number_of_days[0]
root=Tk()
c=Canvas(root,width=1000,height=1000,bg='black')
c.pack()
c.create_text(100,50,anchor='w',fill='orange',\
              font='Arial 28 bold underline',text='My Count Calendar\n')
events=get_events()
today=date.today()
vertical_space=100
events.sort(key=lambda x:x[1])
for event in events:
    event_name=event[0]
    days_until=days_between_dates(event[1],today)
    if(days_until=='0:00:00'):
        days_until='0'
    display='It is %s days until %s'%(days_until,event_name)
    if(int(days_until)<=7):
        text_col='red'
    else:
        text_col='lightblue'
    c.create_text(100,vertical_space,anchor='w',fill=text_col,\
                  font='Arial 28 bold',text=display)
    
    vertical_space=vertical_space+40
root.mainloop()

events.txt:

事情,日/月/年的后两位
如:
程序员节,24/10/24

你可能感兴趣的:(python)