Reputation: 45
I was making a countdown calendar that checks how many days left, name and info, but it is the condition that changes the colour for if more than 50 days part that doesn't work. Can you guys check it?. It checks how many left for each festival. Also as u can see If I change the date to today of any festival, it shows 1 Day left until
. I want to fix that
Here is the text from festivals.txt
Dashain,14/10/21,This is Nepal Main Festival
Tihar,6/11/21,Festival of Lights
Christmas,25/12/21,Jingle Bells
New Year,1/01/22,The Day of the new year
from tkinter import Tk, Canvas, simpledialog, messagebox
from datetime import date, datetime
# function get_events is to get the celebration events
def get_events():
list_events = []
with open('festivals.txt') as file:
for line in file:
line1 = line.rstrip('\n')
global current_event
current_event = line1.split(',')
current_event[1] = datetime.strptime(current_event[1], '%d/%m/%y').date()
list_events.append(current_event)
return list_events
# Function to get the dates between the 2
def days_between_dates(date1, date2):
time_between = str(date1 - date2)
number_of_days = time_between.split(' ')
return number_of_days[0]
# End of Functions
# -----------------------------------------------------------------------------------------------
# Main program starts here
root = Tk()
root.title('Calendar')
# Make Canvas
c = Canvas(root, width=2000, height=800, bg='black')
c.pack()
c.create_text(100, 50, anchor='w', fill='cyan', font=' Times 40 bold italic underline',
text='My Countdown Calendar' )
# Store the functions in variables
events = get_events()
today = date.today()
# Aligning the text, sorting the list
vertical_space = 100
events.sort(key=lambda x: x[1])
horizontal_space = 100
# Main Loop
for event in events:
event_name = event[0]
days_until = days_between_dates(event[1], today)
display = 'It is %s days until %s. %s is about %s' % (days_until, event_name, event_name,event[2])
if (int(days_until) > 10):
text_col = '#c11a2b'
remin = 'Come one %s in coming' % (event_name)
c.create_text(550, 500, anchor='w', fill='purple', font='Courier 20 bold underline', text=remin)
else:
text_col = 'lime'
c.create_text(200, vertical_space, anchor='w', fill=text_col,
font='Calibri 28 bold', text=display)
vertical_space = vertical_space + 30
horizontal_space = horizontal_space + 40
Upvotes: 0
Views: 50
Reputation: 87084
There are a couple of problems with the date calculations.
Firstly you can obtain the number of days using the days
attribute of the timedelta
object returned by date1 - date2
in days_between_dates()
. That function could be written as:
def days_between_dates(date1, date2):
return (date1 - date2).days
This is more succinct and less error prone, and now it will return an integer that is more useful in other calculations.
This also fixes a bug. When the dates are the same and str(date1 - date2)
returns
'0:00:00'
which your code fails to parse.
Regarding the greater than 50 condition, there is no such check in your code. There is a > 10
but your test data does not test that condition. You should add a line with a date within 10 days of the current day.
Finally you will need to handle past events for when the event has already happened. Currently the fixed code would display a negative number of days until the event.
Upvotes: 1