Skip to content

Gtk.Calendar

class — extends Widget, Accessible, Buildable, ConstraintTarget

Displays a Gregorian calendar, one month at a time.

<picture> <source srcset="calendar-dark.png" media="(prefers-color-scheme: dark)"> <img alt="An example GtkCalendar" src="calendar.png"> </picture>

A GtkCalendar can be created with Calendar.new.

The selected date can be retrieved from a GtkCalendar using Calendar.get_date. It can be altered with Calendar.set_date.

To place a visual marker on a particular day, use Calendar.mark_day and to remove the marker, Calendar.unmark_day. Alternative, all marks can be cleared with Calendar.clear_marks.

Users should be aware that, although the Gregorian calendar is the legal calendar in most countries, it was adopted progressively between 1582 and 1929. Display before these dates is likely to be historically incorrect.

Shortcuts and Gestures

GtkCalendar supports the following gestures:

  • Scrolling up or down will switch to the previous or next month.
  • Date strings can be dropped for setting the current day.

CSS nodes

calendar.view
├── header
│   ├── button
│   ├── stack.month
│   ├── button
│   ├── button
│   ├── label.year
│   ╰── button
╰── grid
    ╰── label[.day-name][.week-number][.day-number][.other-month][.today]

GtkCalendar has a main node with name calendar. It contains a subnode called header containing the widgets for switching between years and months.

The grid subnode contains all day labels, including week numbers on the left (marked with the .week-number css class) and day names on top (marked with the .day-name css class).

Day labels that belong to the previous or next month get the .other-month style class. The label of the current day get the .today style class.

Marked day labels get the :selected state assigned.

Constructors

new

@classmethod
def new(cls) -> Widget

Creates a new calendar, with the current date being selected.

Methods

clear_marks

def clear_marks(self) -> None

Remove all visual markers.

get_date

def get_date(self) -> GLib.DateTime

Returns a GDateTime representing the shown year, month and the selected day.

The returned date is in the local time zone.

get_day

def get_day(self) -> int

Gets the day of the selected date.

get_day_is_marked

def get_day_is_marked(self, day: int) -> bool

Returns if the day of the calendar is already marked.

Parameters:

  • day — the day number between 1 and 31.

get_month

def get_month(self) -> int

Gets the month of the selected date.

get_show_day_names

def get_show_day_names(self) -> bool

Returns whether self is currently showing the names of the week days.

This is the value of the Calendar.show-day-names property.

get_show_heading

def get_show_heading(self) -> bool

Returns whether self is currently showing the heading.

This is the value of the Calendar.show-heading property.

get_show_week_numbers

def get_show_week_numbers(self) -> bool

Returns whether self is showing week numbers right now.

This is the value of the Calendar.show-week-numbers property.

get_year

def get_year(self) -> int

Gets the year of the selected date.

mark_day

def mark_day(self, day: int) -> None

Places a visual marker on a particular day of the current month.

Parameters:

  • day — the day number to mark between 1 and 31.

select_day

def select_day(self, date: GLib.DateTime) -> None

:::warning Deprecated since 4.20 This API is deprecated. :::

Switches to date's year and month and select its day.

Parameters:

  • date — a GDateTime representing the day to select

set_date

def set_date(self, date: GLib.DateTime) -> None

Switches to date's year and month and selects its day.

Parameters:

  • date — a GDateTime representing the day to select

set_day

def set_day(self, day: int) -> None

Sets the day for the selected date.

The new date must be valid. For example, setting the day to 31 when the month is February will fail.

Parameters:

  • day — The desired day for the selected date (as a number between 1 and 31).

set_month

def set_month(self, month: int) -> None

Sets the month for the selected date.

The new date must be valid. For example, setting the month to 1 (February) when the day is 31 will fail.

Parameters:

  • month — The desired month for the selected date (as a number between 0 and 11).

set_show_day_names

def set_show_day_names(self, value: bool) -> None

Sets whether the calendar shows day names.

Parameters:

  • value — Whether to show day names above the day numbers

set_show_heading

def set_show_heading(self, value: bool) -> None

Sets whether the calendar should show a heading.

The heading contains the current year and month as well as buttons for changing both.

Parameters:

  • value — Whether to show the heading in the calendar

set_show_week_numbers

def set_show_week_numbers(self, value: bool) -> None

Sets whether week numbers are shown in the calendar.

Parameters:

  • value — whether to show week numbers alongside the days

set_year

def set_year(self, year: int) -> None

Sets the year for the selected date.

The new date must be valid. For example, setting the year to 2023 when the date is February 29 will fail.

Parameters:

  • year — The desired year for the selected date (within GLib.DateTime limits, i.e. from 0001 to 9999).

unmark_day

def unmark_day(self, day: int) -> None

Removes the visual marker from a particular day.

Parameters:

  • day — the day number to unmark between 1 and 31.

Properties

date

date: GLib.DateTime  # read/write

The selected date.

This property gets initially set to the current date.

day

day: int  # read/write

:::warning Deprecated since 4.20 This API is deprecated. :::

The selected day (as a number between 1 and 31).

month

month: int  # read/write

:::warning Deprecated since 4.20 This API is deprecated. :::

The selected month (as a number between 0 and 11).

This property gets initially set to the current month.

show_day_names

show_day_names: bool  # read/write

Determines whether day names are displayed.

show_heading

show_heading: bool  # read/write

Determines whether a heading is displayed.

show_week_numbers

show_week_numbers: bool  # read/write

Determines whether week numbers are displayed.

year

year: int  # read/write

:::warning Deprecated since 4.20 This API is deprecated. :::

The selected year.

This property gets initially set to the current year.

Signals

day-selected

def on_day_selected(self) -> None: ...

Emitted when the user selects a day.

next-month

def on_next_month(self) -> None: ...

Emitted when the user switches to the next month.

next-year

def on_next_year(self) -> None: ...

Emitted when user switches to the next year.

prev-month

def on_prev_month(self) -> None: ...

Emitted when the user switches to the previous month.

prev-year

def on_prev_year(self) -> None: ...

Emitted when user switches to the previous year.