calendar-popup/tkinter-caldav-calendar.py
Anish Lakhwara 900eb2f259 comments
2022-12-17 01:21:18 +10:00

63 lines
1.8 KiB
Python

from tkinter import *
from tkinter import ttk
from pydantic import ValidationError
from tkcalendar import Calendar
from datetime import datetime
from os import listdir
from ical.calendar_stream import IcsCalendarStream
### TKINTER Setup
root = Tk()
root.geometry("+0+0") # TODO set location of tkinter dialog
root.attributes("-type", "dialog")
style = ttk.Style(root)
style.theme_use("clam")
frame = ttk.Frame(root)
frame.grid()
calendar = Calendar(frame)
calendar.grid(row=0,columnspan=7)
calendar.calevent_create(date=datetime(2022, 12, 16), text="today")
### ICS setup
# List calendar folder (~/.calendar/)
calendar_folder = '/home/anish/.calendar/'
# Recurse through each folder
for c in listdir(calendar_folder):
files = listdir(calendar_folder + c)
full_text: str = ""
for file in files:
try:
with open(calendar_folder + c + "/" + file, 'r') as in_file:
cal = IcsCalendarStream.calendar_from_ics(in_file.read())
for e in cal.events:
# Add all events to our calendar widget
calendar.calevent_create(date=e.dtstart, text=e.summary)
except ValidationError:
print(f"Could not parse file: {calendar_folder}{c}")
agenda_var = StringVar()
agenda_var.set("")
def update_agenda(_):
date = calendar.selection_get()
new_agenda = calendar.get_calevents(date)
if new_agenda:
new_agenda = calendar.calevent_cget(new_agenda[0], "text") # TODO list *all* events
agenda_var.set(new_agenda)
root.update_idletasks()
else:
agenda_var.set("")
agenda_label = ttk.Label(frame, textvariable=agenda_var)
agenda_label.grid(rowspan=13, columnspan=7) # TODO make agenda bigger to accomodate multiple events
update_agenda("") # initial set of agenda
root.bind("<<CalendarSelected>>", update_agenda)
root.mainloop()