在 python 中创建一个带有事件的一个月日历

met*_*ta4 7 python calendar matplotlib

我想创建一个一个月的日历,我可以打印出来并交给已经有活动的普通人。

我想做一些类似于WinCalendar所做的事情。我会使用那个程序,但评论提到了粗略的 DLL、注册表项和启动默认值。不应该有一个python库来做到这一点吗?

我可以使用 matplotlib 创建我想要的内容,如下所示。

import calendar
import matplotlib.pyplot as plt

calendar.setfirstweekday(6) # Sunday is 1st day in US
w_days = 'Sun Mon Tue Wed Thu Fri Sat'.split()
m_names = '''
January February March April
May June July August
September October November December'''.split()

class MplCalendar(object):
    def __init__(self, year, month):
        self.year = year
        self.month = month
        self.cal = calendar.monthcalendar(year, month)
        # monthcalendar creates a list of lists for each week
        # Save the events data in the same format
        self.events = [[[] for day in week] for week in self.cal]

    def _monthday_to_index(self, day):
        'The index of the day in the list of lists'
        for week_n, week in enumerate(self.cal):
            try:
                i = week.index(day)
                return week_n, i
            except ValueError:
                pass
         # couldn't find the day
        raise ValueError("There aren't {} days in the month".format(day))

    def add_event(self, day, event_str):
        'insert a string into the events list for the specified day'
        week, w_day = self._monthday_to_index(day)
        self.events[week][w_day].append(event_str)

    def show(self):
        'create the calendar'
        f, axs = plt.subplots(len(self.cal), 7, sharex=True, sharey=True)
        for week, ax_row in enumerate(axs):
            for week_day, ax in enumerate(ax_row):
                ax.set_xticks([])
                ax.set_yticks([])
                if self.cal[week][week_day] != 0:
                    ax.text(.02, .98,
                            str(self.cal[week][week_day]),
                            verticalalignment='top',
                            horizontalalignment='left')
                contents = "\n".join(self.events[week][week_day])
                ax.text(.03, .85, contents,
                        verticalalignment='top',
                        horizontalalignment='left',
                        fontsize=9)

        # use the titles of the first row as the weekdays
        for n, day in enumerate(w_days):
            axs[0][n].set_title(day)

        # Place subplots in a close grid
        f.subplots_adjust(hspace=0)
        f.subplots_adjust(wspace=0)
        f.suptitle(m_names[self.month] + ' ' + str(self.year),
                   fontsize=20, fontweight='bold')
        plt.show()
Run Code Online (Sandbox Code Playgroud)

然后我可以创建一个 MplCalendar 对象,添加事件并显示如下。

from matplotlib_calendar import MplCalendar
import matplotlib_calendar

feb = MplCalendar(2017, 2) #2017, February
feb.add_event(1, '1st day of February')
feb.add_event(5, '         1         2         3         4         5         6')
feb.add_event(5, '123456789012345678901234567890123456789012345678901234567890')
feb.add_event(18, 'OSLL Field Maintenance Day')
feb.add_event(18, 'OSLL Umpire Mechanics Clinic')
feb.add_event(20, 'Presidents day')
feb.add_event(25, 'OSLL Opening Day')
feb.add_event(28, 'T-Ball Angels vs Dirtbags at OSLL')
feb.show()
Run Code Online (Sandbox Code Playgroud)

这将生成一个看起来像这样的日历。

示例二月 Matplotlib 日历

我在这里重新发明轮子吗?我尝试了一堆相关的 Google 搜索,但找不到任何内容。

Bob*_*ear 0

由于没有人回答这个问题,而且它已经有近 5000 次浏览,我认为这个 6 年前的问题应该有一些东西。所以,问题是

我在这里重新发明轮子吗?

虽然这个问题通常会导致错误的固执己见的答案,但这个问题实际上只在生产情况下才重要。在 OP 的情况下,该项目是个人的,并向公众开放,供感兴趣的人使用。即使 6 年前,Python 中还有其他事件日历,重新创建他们的解决方案可以让您学习或练习构建解决方案所涉及的思维过程。虽然代码本身可能不会教给您有关该语言的任何新知识,但它仍然是一项值得做的智力练习。

简而言之,六年前,不,你可能没有重新发明,即使你正在解决一个问题,并且你想使用不同的路线来解决这个问题。

总的来说,我不喜欢“重新发明轮子”这个词,因为轮子本身一直在被重新发明,如果不这样做,我们就没有其他办法可以泄气。总有一些东西需要学习,如果它不是关于代码或语言,那么它是关于如何分解、解决问题和语法选择的思维过程(为什么不使用继承,为什么选择一种算法而不是另一种算法...... .等)。