Nahmid
Nahmid

Reputation: 113

Is there a way to add calevents to DateEntry in tkcalendar?

I am trying to find a way to highlight specific dates on tkcalendar's DateEntry class.

This is running on Python 3. It works successfully with tkcalendar's Calendar class, but does not seem to apply to DateEntry class.

import tkinter as tk
from tkinter import ttk 
from tkcalendar import Calendar, DateEntry

window = tk.Tk()
cal = DateEntry(window)
date = cal.datetime.today() + cal.timedelta(days=2)
cal.calevent_create(date, 'Hello World', 'message')
cal.tag_config('message', background='red', foreground='yellow')
cal.pack()

window.mainloop()

This works if we define cal=Calendar(window), but fails whenever I try to switch it over to DateEntry.

Copy Comment: Changing cal to a Calendar object and then adding:

de=DateEntry(window)  
de.pack()  
de.bind("<<DateEntrySelected>>", cal.calevent_create(date, 'Hello World', 'message'))  

doesn't seem to be working for me... I just end up getting a

TypeError: 'int' object is not callable 

whenever I try to select a date.

Upvotes: 0

Views: 3648

Answers (1)

stovfl
stovfl

Reputation: 15513

Question: Is there a way to add calevents to DateEntry in tkcalendar?

No, DateEntry is for selecting one Date. Calendar is for holding Calendar Events based on Date.


You have to bind("<<DateEntrySelected>>", ... and in the def callback(... do <ref to Calendar>.calevent_create(<selected date>, 'Hello ...'

import tkinter as tk
from tkcalendar import Calendar, DateEntry

window = tk.Tk()

def date_entry_selected(event):
    w = event.widget
    date = w.get_date()
    print('Selected Date:{}'.format(date))
    # <ref to Calendar>.calevent_create(date, 'Hello ...`)
    cal.calevent_create(date, 'Hello ...')

cal = Calendar(window, selectmode='day', year=2019, month=10, day=28)
cal.pack(fill="both", expand=True)

de=DateEntry(window)  
de.pack()  
de.bind("<<DateEntrySelected>>", date_entry_selected)  

window.mainloop()

Upvotes: 1

Related Questions