跳转至

DatePickerDialog

ttkbootstrap.dialogs.dialogs.DatePickerDialog

A dialog that displays a calendar popup and returns the selected date as a datetime object.

The current date is displayed by default unless the startdate parameter is provided.

The month can be changed by clicking the chevrons to the left and right of the month-year title.

Left-click the arrow to move the calendar by one month. Right-click the arrow to move the calendar by one year. Right-click the title to reset the calendar to the start date.

The starting weekday can be changed with the firstweekday parameter for geographies that do not start the calendar on Sunday, which is the default.

The widget grabs focus and all screen events until released. If you want to cancel a date selection, click the 'X' button at the top-right corner of the widget.

The bootstyle api may be used to change the style of the widget. The available colors include -> primary, secondary, success, info, warning, danger, light, dark.

__init__(self, parent=None, title=' ', firstweekday=6, startdate=None, bootstyle='primary') special

Parameters:

Name Type Description Default
parent Widget

The parent widget; the popup will appear to the bottom-right of the parent widget. If no parent is provided, the widget is centered on the screen.

None
title str

The text that appears on the titlebar.

' '
firstweekday int

Specifies the first day of the week. 0=Monday, 1=Tuesday, etc...

6
startdate datetime

The date to be in focus when the widget is displayed.

None
bootstyle str

The following colors can be used to change the color of the title and hover / pressed color -> primary, secondary, info, warning, success, danger, light, dark.

'primary'
Source code in ttkbootstrap/dialogs/dialogs.py
def __init__(
    self,
    parent=None,
    title=" ",
    firstweekday=6,
    startdate=None,
    bootstyle=PRIMARY,
):
    """
    Parameters:

        parent (Widget):
            The parent widget; the popup will appear to the
            bottom-right of the parent widget. If no parent is
            provided, the widget is centered on the screen.

        title (str):
            The text that appears on the titlebar.

        firstweekday (int):
            Specifies the first day of the week. 0=Monday,
            1=Tuesday, etc...

        startdate (datetime):
            The date to be in focus when the widget is
            displayed.

        bootstyle (str):
            The following colors can be used to change the color of
            the title and hover / pressed color -> primary,
            secondary, info, warning, success, danger, light, dark.
    """
    self.parent = parent
    self.root = ttk.Toplevel(
        title=title,
        transient=self.parent,
        resizable=(False, False),
        topmost=True,
        minsize=(226, 1),
        iconify=True,
    )
    self.firstweekday = firstweekday
    self.startdate = startdate or datetime.today().date()
    self.bootstyle = bootstyle or PRIMARY

    self.date_selected = self.startdate
    self.date = startdate or self.date_selected
    self.calendar = calendar.Calendar(firstweekday=firstweekday)

    self.titlevar = ttk.StringVar()
    self.datevar = ttk.IntVar()

    self._setup_calendar()
    self.root.grab_set()
    self.root.wait_window()