tkinter tkcalendar 显示事件 [英] tkinter tkcalendar to display events

查看:15
本文介绍了tkinter tkcalendar 显示事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 tkcalendar 在我的 python tkinter 应用程序中显示一些事件.我有一个事件数据库,我将它们放在日历中,如下例所示(我创建了一个事件字典来显示案例).在这个例子中,我遍历所有事件并将它们全部放入日历

I am trying to use tkcalendar to display some events in my python tkinter application. I have a database of events and i put them in the calendar as in the following example (I have created a dictionary of events to show the case). In this example I loop over all events and put them all in the calendar

import tkinter as tk
import tkinter.ttk as ttk
from tkinter import *
import tkcalendar
from tkcalendar import Calendar, DateEntry
import datetime

root = tk.Tk()

events={'2018-09-28':('London','meeting'),\
    '2018-08-15':('Paris','meeting'),\
    '2018-07-30':('New York','meeting')}

cal = Calendar(root, selectmode='day', year=2018, month=8)

for k in events.keys():
    date=datetime.datetime.strptime(k,"%Y-%m-%d").date()
    cal.calevent_create(date, events[k][0], events[k][1])

cal.tag_config('meeting', background='red', foreground='yellow')
cal.pack(fill="both", expand=True)

root.mainloop()

到目前为止一切正常.问题是事件数据库非常大并且会扩展数年.理想情况下,我只想创建正在显示的月份的事件.我需要检测用户何时单击下个月"和上个月"(tkcalendar 附带的标准按钮):

Up to now everything fine. The problem is that the database of events is quite large and expands several years. Ideally I would want to create only the events of the month being displayed. I need to detect when the user clicks on "Next month" and "Previous Month" (the standard buttons that come with the tkcalendar):

并为显示的月份创建事件.这可能吗?

and create the events for the month being displayed. Is that even possible?

在此先非常感谢您

推荐答案

您可以创建一个继承自 Calendar 的类,并重新定义下个月"和上个月"按钮的回调,以便它们生成一个虚拟事件'<>'.然后将此事件绑定到显示当月事件的函数.

You can create a class inheriting from Calendar and redefine the callbacks of the "Next month" and "Previous Month" buttons so that they generate a virtual event '<<CalendarMonthChanged>>'. Then bind this event to a function that displays the current month events.

代码如下:

from tkcalendar import Calendar
from tkinter import Tk


class MyCalendar(Calendar):

    def _next_month(self):
        Calendar._next_month(self)
        self.event_generate('<<CalendarMonthChanged>>')

    def _prev_month(self):
        Calendar._prev_month(self)
        self.event_generate('<<CalendarMonthChanged>>')

    def _next_year(self):
        Calendar._next_year(self)
        self.event_generate('<<CalendarMonthChanged>>')

    def _prev_year(self):
        Calendar._prev_year(self)
        self.event_generate('<<CalendarMonthChanged>>')

    def get_displayed_month_year(self):
        return self._date.month, self._date.year


def on_change_month(event):
    # remove previously displayed events
    cal.calevent_remove('all')
    year, month = cal.get_displayed_month_year()
    # display the current month events 
    # ...
    print(year, month)

root = Tk()
cal = MyCalendar(root)
cal.pack()

cal.bind('<<CalendarMonthChanged>>', on_change_month)

root.mainloop()

这篇关于tkinter tkcalendar 显示事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆