A simple Calendar class for flet. I needed a calendar so I made one. I hope someone finds it useful.
Python
x
160
160
1
import flet as ft
2
import datetime
3
import calendar
4
from calendar import HTMLCalendar
5
from dateutil import relativedelta
6
7
'''
8
FletCalendar in Python.
9
10
Flet does not have one so I needed to make my own
11
and I thought I would share.
12
13
Author: C. Nichols <mohawke@gmail.com>
14
License: WTFPL
15
WTFPL — Do What the Fuck You Want to Public License
16
17
Requirements: You need to install the following.
18
19
pip install flet
20
21
I suggest you use a Python virtual environment.
22
That will keep your system Python clean and make
23
it easier to manage you Flet project.
24
25
https://docs.python.org/3/library/venv.html
26
'''
27
28
class FletCalendar(ft.UserControl):
29
30
def __init__(self, page):
31
super().__init__()
32
33
self.page = page
34
self.get_current_date()
35
self.set_theme()
36
37
# Init the container control.
38
self.calendar_container = ft.Container(width=355, height=300,
39
padding=ft.padding.all(2),
40
border=ft.border.all(2, self.border_color),
41
border_radius=ft.border_radius.all(10),
42
alignment=ft.alignment.bottom_center)
43
self.build() # Build the calendar.
44
self.output = ft.Text() # Add output control.
45
46
def get_current_date(self):
47
'''Get the initial current date'''
48
today = datetime.datetime.today()
49
self.current_month = today.month
50
self.current_day = today.day
51
self.current_year = today.year
52
53
def selected_date(self, e):
54
'''User selected date'''
55
self.output.value = e.control.data
56
self.output.update()
57
#return e.control.data
58
59
def set_current_date(self):
60
'''Set the calendar to the current date.'''
61
today = datetime.datetime.today()
62
self.current_month = today.month
63
self.current_day = today.day
64
self.current_year = today.year
65
self.build()
66
self.calendar_container.update()
67
68
def get_next(self, e):
69
'''Move to the next month.'''
70
current = datetime.date(self.current_year, self.current_month, self.current_day)
71
add_month = relativedelta.relativedelta(months=1)
72
next_month = current + add_month
73
74
self.current_year = next_month.year
75
self.current_month = next_month.month
76
self.current_day = next_month.day
77
self.build()
78
self.calendar_container.update()
79
80
def get_prev(self, e):
81
'''Move to the previous month.'''
82
current = datetime.date(self.current_year, self.current_month, self.current_day)
83
add_month = relativedelta.relativedelta(months=1)
84
next_month = current - add_month
85
self.current_year = next_month.year
86
self.current_month = next_month.month
87
self.current_day = next_month.day
88
self.build()
89
self.calendar_container.update()
90
91
def get_calendar(self):
92
'''Get the calendar from the calendar module.'''
93
cal = HTMLCalendar()
94
return cal.monthdayscalendar(self.current_year, self.current_month)
95
96
def set_theme(self, border_color=ft.colors.PINK_700,
97
text_color=ft.colors.PINK_50,
98
current_day_color=ft.colors.PINK_700):
99
self.border_color = border_color
100
self.text_color = text_color
101
self.current_day_color = current_day_color
102
103
def build(self):
104
'''Build the calendar for flet.'''
105
current_calendar = self.get_calendar()
106
107
str_date = '{0} {1}, {2}'.format(calendar.month_name[self.current_month], self.current_day, self.current_year)
108
109
date_display = ft.Text(str_date, text_align='center', size=20, color=self.text_color)
110
next_button = ft.Container( ft.Text('>', text_align='right', size=20, color=self.text_color), on_click=self.get_next )
111
div = ft.Divider(height=1, thickness=2.0, color=self.border_color)
112
prev_button = ft.Container( ft.Text('<', text_align='left', size=20, color=self.text_color), on_click=self.get_prev )
113
114
calendar_column = ft.Column([ft.Row([prev_button, date_display, next_button], alignment=ft.MainAxisAlignment.SPACE_EVENLY,
115
vertical_alignment=ft.CrossAxisAlignment.CENTER, height=40, expand=False), div],
116
spacing=2, width=355, height=330, alignment=ft.MainAxisAlignment.START, expand=False)
117
# Loop weeks and add row.
118
for week in current_calendar:
119
week_row = ft.Row(alignment=ft.MainAxisAlignment.CENTER)
120
# Loop days and add days to row.
121
for day in week:
122
if day > 0:
123
is_current_day_font = ft.FontWeight.W_300
124
is_current_day_bg = ft.colors.TRANSPARENT
125
display_day = str(day)
126
if len(str(display_day)) == 1: display_day = '0%s' % display_day
127
if day == self.current_day:
128
is_current_day_font = ft.FontWeight.BOLD
129
is_current_day_bg = self.current_day_color
130
131
day_button = ft.Container(content=ft.Text(str(display_day), weight=is_current_day_font, color=self.text_color),
132
on_click=self.selected_date, data=(self.current_month, day, self.current_year),
133
width=40, height=40, ink=True, alignment=ft.alignment.center,
134
border_radius=ft.border_radius.all(10),
135
bgcolor=is_current_day_bg)
136
else:
137
day_button = ft.Container(width=40, height=40, border_radius=ft.border_radius.all(10))
138
139
week_row.controls.append(day_button)
140
141
# Add the weeks to the main column.
142
calendar_column.controls.append(week_row)
143
# Add column to our page container.
144
self.calendar_container.content = calendar_column
145
return self.calendar_container
146
147
def main(page: ft.Page):
148
149
page.theme = ft.theme.Theme(color_scheme_seed=ft.colors.PINK)
150
page.dark_theme = ft.theme.Theme(color_scheme_seed=ft.colors.PINK)
151
152
# Instantiate the FletCalendar class.
153
mycal = FletCalendar(page)
154
155
# Add to our application.
156
page.add(mycal, mycal.output)
157
158
page.update()
159
160
ft.app(target=main)