FletのDataPickerはカレンダー設定を使うときに便利なコンポーネントです。
import datetime
import flet as ft
def main(page: ft.Page):
def change_date(e):
print(e)
print(f"Date picker changed, value is {date_picker.value}")
def date_picker_dismissed(e):
print(f"Date picker dismissed, value is {date_picker.value}")
today = datetime.datetime.now().date()
next_year = today + datetime.timedelta(days=365)
date_picker = ft.DatePicker(
on_change=change_date,
on_dismiss=date_picker_dismissed,
first_date=today,
last_date=next_year,
)
page.overlay.append(date_picker)
date_button = ft.ElevatedButton(
"Pick date",
icon=ft.icons.CALENDAR_MONTH,
on_click=lambda _: date_picker.pick_date(),
)
page.add(date_button)
ft.app(target=main)
しかし、デフォルトの地域がUSになっているため、dd/mm/yyyyという表示になります。
今までは表示を日本式(yyyy/mm/dd)に変えても入力チェックがUS式のため、日本式で使用することができませんでした。
Ver0.22.0から、言語設定ができるように
Flet 0.22.0から、Locale設定(locale_configuration)ができるようになりました。
locale_configuration
Fletのドキュメント1に記載されています。
A locale configuration for the app. Value is an instance of LocaleConfiguration class which has the following properties:
- supported_locales - a list of Locales that the app plans to support. If the provided value is None or list is empty, this property internally defaults to [Locale("en", "US")] (American English locale) by default.
- current_locale - the current Locale of the app. If the provided locale is not present in supported_locales, then this property will be set to supported_locales[0] (the first item of the list).
Locale class has the following properties:
- language_code - the language code of the locale.
- country_code - the country code of the locale.
- script_code - the script code of the locale.
supported_localesはアプリが対応する言語を設定します。
複数設定が可能です。
current_localeは現時点の言語(≒アプリ起動時の言語)を設定します。
supported_localesで設定した言語から設定します。2
デフォルトはen,USです。
言語を設定するときは言語コードと国コードを指定します。
# サポート言語を日本語と英語で、初期値を日本語に設定する
page.locale_configuration = ft.LocaleConfiguration(
supported_locales=[ft.Locale("ja", "JP"), ft.Locale("en", "US")], current_locale=ft.Locale("ja", "JP")
)
サンプルコード
詳しい実装方法は以下のブログに記載しています。
Locale設定をして、DatePickerを使用してみます。3
import datetime
import flet as ft
def main(page: ft.Page):
# 日本語設定
page.locale_configuration = ft.LocaleConfiguration(
supported_locales=[
ft.Locale("ja", "JP"),
ft.Locale("en", "US")
],
current_locale=ft.Locale("ja", "JP")
)
def change_date(e):
print(e)
print(f"Date picker changed, value is {date_picker.value}")
def date_picker_dismissed(e):
print(f"Date picker dismissed, value is {date_picker.value}")
today = datetime.datetime.now().date()
next_year = today + datetime.timedelta(days=365)
date_picker = ft.DatePicker(
on_change=change_date,
on_dismiss=date_picker_dismissed,
first_date=today,
last_date=next_year,
)
page.overlay.append(date_picker)
date_button = ft.ElevatedButton(
"Pick date",
icon=ft.icons.CALENDAR_MONTH,
on_click=lambda _: date_picker.pick_date(),
)
page.add(date_button)
ft.app(target=main)
Localeを日本語に設定すると、カレンダーも日本式になります。
使ってみて
いままで「日本語式だったらな~」と思うことがあったのでこのアップデートはとてもありがたかったです。
今後もどんどんアップデートする予定とのことなのでより活用していきたいと思います。
参考ページ