python输出一个月日历表_用python创建一个包含事件的一个月日历

本文介绍如何使用Python的matplotlib库创建一个可打印的月份日历,并添加事件。通过定义一个MplCalendar类,可以插入事件并展示带有事件的定制日历。示例代码展示了创建和展示日历的过程。
摘要由CSDN通过智能技术生成

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

我想做一些类似于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()

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

^{pr2}$

这将生成一个类似这样的日历。在

2a5db13a6e0abbf04b6c9a1440c37392.png

我是在重新发明轮子吗?我尝试了一堆相关的谷歌搜索,但什么也找不到。在

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值