ふらふら Diary (仮)

興味のあることを適当に書いていく感じです

Pythonでカレンダー表示

カレンダーを表示

>>> import calendar
>>> my_calendar = calendar.TextCalendar(firstweekday=6)
>>> my_calendar_string = my_calendar.formatmonth(2018, 11)
>>> print(my_calendar_string)

   November 2018
Su Mo Tu We Th Fr Sa
             1  2  3
 4  5  6  7  8  9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
25 26 27 28 29 30

これで普通にカレンダー表示できたけど、GUIツールでは表示できなかった。
なので別の方法を検討。調べていくうちに、日をリストにすればいいのではと思った。

>>> my_calendar.monthdayscalendar(2018, 11)

[[0, 0, 0, 0, 1, 2, 3],
 [4, 5, 6, 7, 8, 9, 10],
 [11, 12, 13, 14, 15, 16, 17],
 [18, 19, 20, 21, 22, 23, 24],
 [25, 26, 27, 28, 29, 30, 0]]

日付が入らないところは0になるみたい。
日にちを一つずつ扱っていけばいい感じに表示できそう。