Python Google日历API获取提醒

2024-04-19 11:28:36 发布

您现在位置:Python中文网/ 问答频道 /正文

嗨,我正试图从谷歌日历api获取某个日期范围的提醒,即从今天到后天。但不幸的是,当我调用提醒时,它给了我一个索引器。请帮我解决这个问题。提前谢谢。Iam附加下面的代码,然后是错误

from tkinter import *
import time
import datetime
from PIL import Image, ImageTk
import requests
import calendar
from apiclient.discovery import build
from google_auth_oauthlib.flow import InstalledAppFlow
import pickle

class Calendar(Frame):                 
    def __init__(self, parent):
        super(Calendar, self).__init__(bg='black')
        self.calendars()
        self.reminders()

    def calendars(self):    
        cal = calendar.month(2019,10)
        self.calendarlb = Label(self, text=cal, font="Helvetica 12", bg='black', fg='white')
        self.calendarlb.pack(side=TOP, anchor=N, fill=BOTH, expand=YES)

    def reminders(self):
        scopes = ['https://www.googleapis.com/auth/calendar']
        #flow = InstalledAppFlow.from_client_secrets_file("client_secret.json", scopes=scopes)
        #credentials = flow.run_console()
        #pickle.dump(credentials, open("token.pkl", "wb"))
        credentials = pickle.load(open("token.pkl", "rb"))
        service = build("calendar", "v3" , credentials=credentials)
        now = datetime.datetime.utcnow().isoformat() + 'Z'
        diff = datetime.timedelta(2)
        Maxtime = now + str(diff)
        result = service.calendarList().list().execute()
        calendar_id = result['items'][1]['id']
        result_new = service.events().list(calendarId=calendar_id, timeMin=now, timeMax=Maxtime).execute()
        result_newer = result_new['items'][0]['summary']
        result_newer1 = result_new['items'][1]['summary']
        result_newer2 = result_new['items'][2]['summary']
        result_newer3 = result_new['items'][3]['summary']
        #titlelb = Label(self, text="To-do List", font="Helvetica 30", bg='black', fg='white')
        #titlelb.pack(side=TOP, anchor=NW)
        result_newer1lb = Label(self, text=result_newer1, font="helvetica 15", bg='black', fg='white')
        result_newer1lb.pack(side=TOP,anchor=N)
        result_newer2lb = Label(self, text=result_newer2, font="helvetica 15", bg='black', fg='white')
        result_newer2lb.pack(side=TOP,anchor=N)
        result_newer3lb = Label(self, text=result_newer3, font="helvetica 15", bg='black', fg='white')
        result_newer3lb.pack(side=TOP,anchor=N)
        #self.after(1000,self.reminders)

错误:-

Traceback (most recent call last): 
File "[...]\Python36\SmartMirror\SmartMirror2.py", line 218, in w = FullscreenWindow() 
File "[...]\Python36\SmartMirror\SmartMirror2.py", line 204, in init self.calendar = Calendar(self.bottomFrame) 
File "[...]\Python36\SmartMirror\SmartMirror2.py", line 149, in init self.reminders() 
File "[...]\Python36\SmartMirror\SmartMirror2.py", line 170, in reminders result_newer = result_new['items'][0]['summary'] IndexError: list index out of range

Tags: textfromimportselfnewitemsresultcalendar
1条回答
网友
1楼 · 发布于 2024-04-19 11:28:36

关闭并读取堆栈跟踪:

line 170, in reminders result_newer = result_new['items'][0]['summary'] 
IndexError: list index out of range

IndexError表示脚本试图索引到一个列表(result_new['items'][0]),但在本例中,该列表是空的。你知道吗

重新运行脚本并检查第170行之前result_new['items']的内容,以了解您可能希望如何调整结果对象的解包方式。你知道吗

相关问题 更多 >