问题
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.
回答1:
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 ...'
- Calendar
- DateEntry
- DateEntry - virtual-events
- Tkinter Events and Bindings
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()
来源:https://stackoverflow.com/questions/58591208/is-there-a-way-to-add-calevents-to-dateentry-in-tkcalendar