How to Change tkcalendar date?
As indicated in the documentation, the Calendar
class has a .selection_set()
method to change the selected date. It takes in argument the date to be selected in the form of a datetime.date
, a datetime.datetime
or a string formated according to the locale used in the calendar.
Example:
import tkinter as tk
import tkcalendar as tkc
from datetime import date
root = tk.Tk()
cal = tkc.Calendar(root, year=1900, month=11, day=11)
cal.pack()
tk.Button(root, text="Change date", command=lambda: cal.selection_set(date(2001, 1, 18))).pack()
Alternatively, you can connect a StringVar
to the calendar through the textvariable
option and use it to get and set the selected date:
import tkinter as tk
import tkcalendar as tkc
from datetime import date
root = tk.Tk()
datevar = tk.StringVar(root, "11/11/1990")
cal = tkc.Calendar(root, textvariable=datevar, locale="en_US")
cal.pack()
tk.Button(root, text="Change date", command=lambda: datevar.set("01/18/2001")).pack()