Skip to main content

DatePicker

A date picker (DatePicker) offers a drop-down control that’s optimized for picking a single date from a calendar view where contextual information like the day of the week or fullness of the calendar is important.

Examples

Live demo

Basic DatePicker

from datetime import datetime
import pglet
from pglet import DatePicker, Button, Text
with pglet.page("basic-datepicker") as page:
def button_clicked(e):
t.value = f"DatePickers values are: {dp1.value}, {dp2.value}."
page.update()

now = datetime.now()
t = Text()
b = Button(text='Submit', on_click=button_clicked)
dp1 = DatePicker(label="Start date", value=now, width=150)
dp2 = DatePicker(label="End date", width=150)

page.add(dp1, dp2, b, t)
input()

DatePicker with text input allowed

from datetime import datetime
import pglet
from pglet import DatePicker
with pglet.page("datepicker-allow-text-input") as page:
now = datetime.now()
page.add(
DatePicker(width=150, label="Allow text input", allow_text_input=True),
DatePicker(label="Allow text input with placeholder", placeholder='Select date...', allow_text_input=True, width='25%'),
DatePicker(value=now, label="Required", required=True, allow_text_input=True))

DatePicker with change event

from datetime import datetime
import pglet
from pglet import DatePicker, Text
with pglet.page("datepicker-with-change-event") as page:
def datepicker_changed(e):
t.value = f"DatePicker value changed to {dp.value}"
t.update()

now = datetime.now()
t = Text()
dp = DatePicker(label="Start date", value=now, width=150, on_change=datepicker_changed)

page.add(dp, t)
input()

Properties

NameTypeDefaultDescription
valuedateCurrent value of the DatePicker.
labelstringLabel to display above the DatePicker.
placeholderstringThe short hint displayed in the DatePicker before the user enters a value.
requiredboolfalseDisplay DatePicker as required.
allowTextInputboolfalseWhether the DatePicker allows input a date string directly or not.
borderlessboolfalseWhether or not the DatePicker is borderless.
underlinedboolfalseWhether or not the DatePicker is underlined.
focusedboolfalseWhen set to true the focus is set on the control when it's shown on the page or page opened.

Events

NameDescription
changeFires when the selected date is changed (either selected from dropdown or entered if allowTextInput is enabled.)
focusFires when the control has received focus.
blurFires when the control has lost focus.