我在Python中遇到这个问题。昨天,我在Python的“编码项目”中复制了“倒数日历”代码,并添加了“ events.txt”。我检查了所有问题以确保其正常工作。但是当我运行它时,文本没有显示出来。这是我的文档设置方式:
代码如下:
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)
答案 0 :(得分:0)
我认为有两件事。
current_event = line.split('.')
-我相信这应该是逗号,而不是句点。root.mainloop()
。我发现在进行本练习时,这解决了我的问题。