HandsomeMustach
HandsomeMustach

Reputation: 1

The Calendar Countdown code is not working

I have this problem in Python. Yesterday, I copied the Countdown Calendar code in Coding Projects with Python and added the 'events.txt'. I checked all the problem to make sure it work. But when I ran it, the text isn't show up. Here is how my documents are set up:

documents

Here's the code:

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=800, height=800, bg='black')
c.pack()
c.create_text(100, 50, anchor='w', fill='orange', \
              font='Arial 28 bold underline', text='My Countdown Calendar')

events = get_events()
today = date.today()
vertical_space = 100

for event in events:
    event_name = event[0]
    days_until = days_between_dates(event[1], today)
    display = 'It is %s days until %s' % (days_until, event_name)
    c.create_text(100, vertical_space, anchor='w', fill='lightblue', \
                  font='Arial 28 bold', text=display)    

Upvotes: 0

Views: 574

Answers (2)

Taylor 13reputation
Taylor 13reputation

Reputation: 1

you need root.mainloop() at the end of your code for it to run

Upvotes: 0

ChrisB
ChrisB

Reputation: 11

I think there are a couple things.

  1. current_event = line.split('.') - I believe this should be a comma, not a period.
  2. Like somebody else has mentioned, if using PyCharm, you need to add root.mainloop() to the end of your file.
  3. Make sure your text file doesn't have any additional lines at the end and there are no spaces between the events and the event dates - just a comma.

I found that this fixed my issues when working on this exercise.

Upvotes: 1

Related Questions