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

泪湿孤枕 提交于 2020-08-11 02:31:53

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!