Skip to content

DatePicker

The Date widget allows the user to select a date from a calendar dropdown or manually input a date. The selected date can be accessed and modified through the date property, which is a datetime.date object, or through the date_str property, which is a string representation of the date in the format specified by the format property. The Date widget also provides methods for manipulating the date, such as adding or subtracting days, months, or years.

Examples

Days until

This example demonstrates how to use the Date widget to calculate the number of days until a specific date. The user can select a date from the calendar dropdown, and the app will display the number of days from today’s date until the selected date.

days_until.py
import gooeypie as gp
from datetime import date
def show_days_until(event):
if target_date.date:
days_until = (target_date.date - date.today()).days
days_until_lbl.text = f'{days_until} days to go!'
else:
days_until_lbl.text = ''
app = gp.GooeyPieApp('How long until...')
# Create widgets
target_date = gp.DatePicker()
target_date.minimum_date = date.today()
days_until_lbl = gp.Label()
days_until_lbl.style.font_size = 24
# Add widgets to window
app.add(target_date, 1, 1)
app.add(days_until_lbl, 1, 2)
target_date.on_change(show_days_until)
app.run()

Creating a DatePicker widget

my_date = gp.DatePicker()

Properties

Name Type Description
.date datetime.date object Gets or sets the date of the Date widget. The date should be a Python datetime.date object.
.minimum_date datetime.date object Gets or sets the minimum date that can be selected in the Date widget.
.maximum_date datetime.date object Gets or sets the maximum date that can be selected in the Date widget.
.date_str string Gets or sets the date of the Date widget as a string in the format specified by the format property. Setting this property will update the date property to match the new date.
.width integer Gets or sets the width of the widget in pixels.
.disabled boolean Gets or sets whether the date widget is disabled. When disabled is set to True, the date widget does not respond to user interactions.
.allow_manual_input boolean Gets or sets whether the user is allowed to manually input a date into the date widget. When allow_manual_input is set to True, the user can type a date into the date widget in addition to selecting a date from the calendar. The default value is True.
.format string Gets or sets the format of the date displayed in the date widget. The format should be a string that follows the strftime format codes. The default format is "%Y-%m-%d", which displays the date as "2024-06-01".
.localization string Gets or sets the localization of the date widget. The localization should be a locale string such as "fr_FR" or "en_US". The default localization is the empty string "", which uses the system default locale.

Methods

Name Returns Description
.open() None Opens the calendar dropdown of the date widget, allowing the user to select a date from the calendar.
.set_today() None Sets the date of the date widget to today's date.
.clear() None Clears the date from the date widget, setting the date property to None and the date_str property to an empty string.
.add_days(days) None Adds the specified number of days to the current date of the date widget.
.subtract_days(days) None Subtracts the specified number of days from the current date of the date widget.
.add_months(months) None Adds the specified number of months to the current date of the date widget.
.subtract_months(months) None Subtracts the specified number of months from the current date of the date widget.
.add_years(years) None Adds the specified number of years to the current date of the date widget.
.subtract_years(years) None Subtracts the specified number of years from the current date of the date widget.

Events

This widget supports the following events as well as the standard events listed below.

Name Description
.on_change(event_function) The function to call when the date is changed. This event is triggered whenever the user selects a new date from the calendar or manually inputs a new date into the date widget, as well as when the date is changed programmatically by setting the date property or using the add_days(), add_months(), or add_years() methods.

Standard events

  • .on_click
  • .on_double_click
  • .on_right_click
  • .on_middle_click
  • .on_mouse_down
  • .on_mouse_up
  • .on_mouse_enter
  • .on_mouse_leave
  • .on_key_press
  • .on_focus_gained
  • .on_focus_lost

Styles

Styles for this widget

How to specify colors

  • Colors can be specified as either a hex code (e.g. "#FF0000"), a CSS color name, or "transparent".
  • When specifying a color for a style, you can provide either a single color or two colors separated by a comma. If you provide two colors, the first will be used for light mode and the second for dark mode. For example, my_widget.text_color = "steelblue", "skyblue" would specify steelblue for light mode and sky blue for dark mode.
Name Description Default
.style.date_border_color The color of the border of the date widget. "transparent"
.style.date_border_width The width of the border of the date widget. 2
.style.date_bg_color The background color of the date widget. "transparent"
.style.text_color The color of the text of the date widget.
.style.text_disabled_color The color of the text of the date widget when it is disabled.
.style.date_font_name The font of the text of the date widget. Either an installed font or a generic font name: "serif", "sans-serif", "monospace" or "system" "system"
.style.date_font_size The size of the text of the date widget. 12
.style.date_font_weight The weight of the text of the date widget. Either "normal" or "bold". "normal"
.style.date_font_style The style of the text of the date widget. Either "normal" or "italic". "normal"
.style.open_button_bg_color The background color of the open calendar button of the date widget. "transparent"
.style.open_button_hover_color The background color of the open calendar button of the date widget when hovered over. "transparent"
.style.open_button_icon_color The color of the icon of the open calendar button of the date widget.
.style.month_font_name The font of the month and year in the calendar dropdown. Either an installed font or a generic font name: "serif", "sans-serif", "monospace" or "system" "system"
.style.month_text_color The color of the month and year text in the calendar dropdown.
.style.month_button_bg_color The background color of the month and year buttons in the calendar dropdown.
.style.month_button_hover_color The background color of the month and year buttons in the calendar dropdown when hovered over.
.style.day_text_color The color of the day numbers in the calendar dropdown.
.style.day_hover_color The background color of the day numbers in the calendar dropdown when hovered over.