1. import calendar
  2. #カレンダーの作成に利用する
  3. import datetime
  4. import PySimpleGUI as sg
  5. sg.theme('LightBlue6')
  6. today = datetime.date.today()
  7. def create_layout(cal_date):
  8.     weekday = ['SUN','MON', 'TUE', 'WED', 'THU', 'FRI', 'SAT']
  9.     #曜日の名前を表示できる
  10.     cal = calendar.Calendar(firstweekday=6)
  11.     days = cal.monthdatescalendar(cal_date.year, cal_date.month)
  12.     layout = [[sg.Text(cal_date.year, font=(None, 13, 'bold'))],
  13.               [sg.Push(), sg.Button('<<'), sg.Button('<'), sg.Text(cal_date.month, font=(None, 30)), sg.Button('>'), sg.Button('>>'), sg.Push()]]
  14.     inner = []
  15.     
  16.     for week in weekday:
  17.         inner.append(sg.Text(week, size=(16,3), text_color='white', background_color='black', justification='center'))
  18.     layout.append(inner.copy())#(○,○)で画像の大きさ変更 #colorで好きな色に変更できる
  19.     def date_judgement(i, day):
  20.         if day == today:
  21.             return sg.Text(day.day, size=(16,3), justification='right', text_color='white', background_color='gray')
  22.         elif i == 0 and day.month == cal_date.month:
  23.             return sg.Text(day.day, size=(16,3), justification='right', text_color='red')
  24.         elif i == 6 and day.month == cal_date.month:
  25.             return sg.Text(day.day, size=(16,3), justification='right', text_color='blue')
  26.         elif day.month == cal_date.month:
  27.             return sg.Text(day.day, size=(16,3), justification='right')
  28.         elif i == 0:
  29.             return sg.Text(day.day, size=(16,3), justification='right', text_color='#ff9999')
  30.         elif i == 6:
  31.             return sg.Text(day.day, size=(16,3), justification='right', text_color='#9999ff')
  32.         else:
  33.             return sg.Text(day.day, size=(16,3), justification='right', text_color='#cccccc')
  34.     for row in days:
  35.         inner = []
  36.         for i, day in enumerate(row):
  37.             sg_text = date_judgement(i, day)
  38.             inner.append(sg_text)
  39.         layout.append(inner.copy())
  40.     return layout
  41. def main():
  42.     cal_date = today
  43.     layout = create_layout(cal_date)
  44.     window = sg.Window('Simple Calendar', layout)
  45.     while True:
  46.         event, _ = window.read()
  47.         if event == sg.WIN_CLOSED:
  48.             break
  49.         elif event == '<':
  50.             cal_date = datetime.date(cal_date.year, cal_date.month, 1) - datetime.timedelta(days=1)
  51.         elif event == '>':
  52.             cal_date = datetime.date(cal_date.year, cal_date.month, 28) + datetime.timedelta(days=4)
  53.         elif event == '<<':
  54.             cal_date = datetime.date(cal_date.year - 1, cal_date.month, 1)
  55.         elif event == '>>':
  56.             cal_date = datetime.date(cal_date.year + 1, cal_date.month, 1)
  57.         window.close()
  58.         window = sg.Window('Simple Calendar', create_layout(cal_date))
  59.     window.close()
  60. if __name__ == '__main__':
  61.     main()