class MyCalendar(tk.Tk): def __init__(self, *args, **kwargs): tk.Tk.__init__(self, *args, **kwargs) #Create calendar self.cal = Calendar(self, font="Arial 14", selectmode='day', tooltipforeground='black', tooltipbackground='pink', tooltipalpha='1', tooltipdelay='0') date = self.cal.datetime.today() #Add in events self.cal.c1 = self.cal.calevent_create( date + self.cal.timedelta(days=5), "F**k me in the ass", 'message') self.cal.calevent_create(date, 'Bake cookies at 3am', 'reminder') self.cal.calevent_create(date + self.cal.timedelta(days=-2), 'Date at McDonalds', 'reminder') self.cal.calevent_create(date + self.cal.timedelta(days=3), 'Mine Diamonds with Jimothy', 'message') #Set calendar and adjust colours self.cal.tag_config('reminder', background='red', foreground='yellow') self.cal.pack(fill="both", expand=True) #Set status/tool tip bar self.l2 = tk.Label(self, text="Click on a date to check for events!", width=40) self.l2.pack(side="bottom") #Executes "set_date" when a day is clicked self.cal.bind("<<CalendarSelected>>", self.set_date) def set_date(self, event): #Get the date clicked self.date = self.cal.get_date() #Convert date from string format to datetime object format self.DTObject = datetime.strptime(self.date, '%m/%d/%y') #Get tuple of events on the date clicked self.eventslist = self.cal.get_calevents(self.DTObject) #If there >0 events, display first event on status/tool tip bar if len(self.eventslist) > 0: self.l2.configure( text=self.cal.calevents[self.eventslist[0]]["text"]) #Display no events else: self.l2.configure(text="No events for " + self.date)
def test_calendar_calevents(self): widget = Calendar(self.window) widget.pack() self.window.update() evdate = date.today() + widget.timedelta(days=2) widget.calevent_create(evdate, 'Hello World', 'message') widget.calevent_create(evdate, 'Reminder 2', 'reminder') widget.calevent_create(evdate + widget.timedelta(days=-2), 'Reminder 1', 'reminder') widget.calevent_create(evdate + widget.timedelta(days=3), 'Message', 'message') widget.tag_config('reminder', background='red', foreground='yellow') widget.tag_config('test', background='blue', foreground='white') # get_calevents self.assertEqual(widget.get_calevents(), tuple(i for i in range(4))) self.assertEqual(widget.get_calevents(date=evdate), (0, 1)) self.assertEqual( widget.get_calevents(date=evdate + widget.timedelta(days=-5)), ()) self.assertEqual(widget.get_calevents(tag='message'), (0, 3)) self.assertEqual(widget.get_calevents(tag='message', date=evdate), (0, )) self.assertEqual( widget.get_calevents(tag='message', date=evdate + widget.timedelta(days=-2)), ()) with self.assertRaises(TypeError): widget.get_calevents(date='12/12/2012') # cget / configure self.assertEqual(widget.calevent_cget(1, 'tags'), ['reminder']) self.assertEqual(widget.calevent_cget(0, 'text'), 'Hello World') self.assertEqual(widget.calevent_cget(2, 'date'), evdate + widget.timedelta(days=-2)) widget.calevent_configure(1, tags=['reminder', 'new']) self.assertEqual(widget.calevent_cget(1, 'tags'), ['reminder', 'new']) widget.calevent_configure(1, tags='reminder') self.assertEqual(widget.calevent_cget(1, 'tags'), ['reminder']) widget.calevent_configure(0, text='Hi') self.assertEqual(widget.calevent_cget(0, 'text'), 'Hi') self.assertNotIn(evdate + widget.timedelta(days=5), widget._calevent_dates) widget.calevent_configure(3, date=evdate + widget.timedelta(days=5, minutes=2)) self.assertEqual(widget.calevent_cget(3, 'date'), evdate + widget.timedelta(days=5)) self.assertIn(evdate + widget.timedelta(days=5), widget._calevent_dates) widget.calevent_configure(2, date=evdate) self.assertEqual(widget.calevent_cget(2, 'date'), evdate) with self.assertRaises(ValueError): widget.calevent_cget(1, 'arg') with self.assertRaises(ValueError): widget.calevent_cget(5, 'date') with self.assertRaises(ValueError): widget.calevent_configure(5, text='a') with self.assertRaises(KeyError): widget.calevent_configure(1, test='a') with self.assertRaises(TypeError): widget.calevent_configure(1, date='a') # lower / raise self.assertEqual(widget._calevent_dates[evdate], [0, 1, 2]) widget.calevent_lower(0) self.assertEqual(widget._calevent_dates[evdate], [1, 2, 0]) widget.calevent_lower(1, 2) self.assertEqual(widget._calevent_dates[evdate], [2, 1, 0]) widget.calevent_raise(0) self.assertEqual(widget._calevent_dates[evdate], [0, 2, 1]) widget.calevent_raise(1, 2) self.assertEqual(widget._calevent_dates[evdate], [0, 1, 2]) with self.assertRaises(ValueError): widget.calevent_raise(4) with self.assertRaises(ValueError): widget.calevent_raise(1, 4) with self.assertRaises(ValueError): widget.calevent_lower(4) with self.assertRaises(ValueError): widget.calevent_lower(1, 4) # tags self.assertEqual(set(widget.tag_names()), set(('new', 'message', 'reminder', 'test'))) self.assertEqual(widget.tag_cget('test', 'foreground'), 'white') self.assertEqual(widget.tag_cget('test', 'background'), 'blue') with self.assertRaises(ValueError): widget.tag_cget('hello', 'background') with self.assertRaises(ValueError): widget.tag_cget('test', 'text') with self.assertRaises(ValueError): widget.tag_delete('birthday') widget.tag_delete('message') self.assertEqual(set(widget.tag_names()), set(('new', 'reminder', 'test'))) self.assertEqual(widget.calevent_cget(0, 'tags'), []) # remove widget.calevent_remove(0) self.assertEqual(widget.get_calevents(), tuple(i for i in range(1, 4))) widget.calevent_remove(date=evdate + widget.timedelta(days=3)) self.assertEqual(widget.get_calevents(), tuple(i for i in range(1, 4))) widget.calevent_remove(date=evdate + widget.timedelta(days=5)) self.assertEqual(widget.get_calevents(), tuple(i for i in range(1, 3))) widget.calevent_remove(tag='reminder') self.assertEqual(widget.get_calevents(), ()) widget.calevent_create(evdate, 'Hello World', 'message') widget.calevent_create(evdate, 'Reminder 2', 'reminder') widget.calevent_create(evdate + widget.timedelta(days=-2), 'Reminder 1', 'reminder') widget.calevent_create(evdate + widget.timedelta(days=3), 'Message', 'message') self.window.update() self.assertEqual(widget.get_calevents(), tuple(i for i in range(4))) widget.calevent_remove(tag='reminder', date=evdate) self.assertEqual(widget.get_calevents(), (0, 2, 3)) widget.calevent_remove(3, tag='reminder', date=evdate) self.assertEqual(widget.get_calevents(), (0, 2)) widget.calevent_remove('all') self.assertEqual(widget.get_calevents(), ())