python-3.xtkintertkcalendar

How to use Tkcalendar virtual events?


Still I’m confused on that tkcalendar virtual events usage to bind user actions on the calendar days selections. Making something like follow,

calendar.bind("<<CalendarSelected>>", print(calendar.get_date())

That print the default date once the I run the script but when I select on the displayed calendar nothing is printed. Normally any selection on any day on the calendar should be printed if was really bound. I did something wrong right ?


Solution

  • You need to use a lambda function instead

    calendar.bind("<<CalendarSelected>>", lambda: print(calendar.get_date())
    

    You code will currently bind the result of the print function as the callback for the bind. Since print returns None, no function will be called.

    A lambda creates an anonymous function.

    If your code gets more complex than a single line, put this code in to a function of its own and use the name of that function in the bind method, for example

    calendar.bind("<<CalendarSelected>>", showDateToUser)