Calendar#

class Calendar(**properties: Any)#

Superclasses: Widget, InitiallyUnowned, Object

Implemented Interfaces: Accessible, Buildable, ConstraintTarget

Displays a Gregorian calendar, one month at a time.

https://docs.gtk.org/gtk4/calendar.png

A GtkCalendar can be created with new.

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

To place a visual marker on a particular day, use mark_day and to remove the marker, unmark_day. Alternative, all marks can be cleared with 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#

class Calendar
classmethod new() Widget#

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

Methods#

class Calendar
clear_marks() None#

Remove all visual markers.

get_date() DateTime#

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

The returned date is in the local time zone.

get_day() int#

Gets the day of the selected date.

Added in version 4.14.

get_day_is_marked(day: int) bool#

Returns if the day of the calendar is already marked.

Parameters:

day – the day number between 1 and 31.

get_month() int#

Gets the month of the selected date.

Added in version 4.14.

get_show_day_names() bool#

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

This is the value of the show_day_names property.

get_show_heading() bool#

Returns whether self is currently showing the heading.

This is the value of the show_heading property.

get_show_week_numbers() bool#

Returns whether self is showing week numbers right now.

This is the value of the show_week_numbers property.

get_year() int#

Gets the year of the selected date.

Added in version 4.14.

mark_day(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(date: DateTime) None#

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

Deprecated since version 4.20: Use set_date instead.

Parameters:

date – a GDateTime representing the day to select

set_date(date: DateTime) None#

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

Added in version 4.20.

Parameters:

date – a GDateTime representing the day to select

set_day(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.

Added in version 4.14.

Parameters:

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

set_month(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.

Added in version 4.14.

Parameters:

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

set_show_day_names(value: bool) None#

Sets whether the calendar shows day names.

Parameters:

value – Whether to show day names above the day numbers

set_show_heading(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(value: bool) None#

Sets whether week numbers are shown in the calendar.

Parameters:

value – whether to show week numbers alongside the days

set_year(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.

Added in version 4.14.

Parameters:

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

unmark_day(day: int) None#

Removes the visual marker from a particular day.

Parameters:

day – the day number to unmark between 1 and 31.

Properties#

class Calendar
props.date: DateTime#

The selected date.

This property gets initially set to the current date.

props.day: int#

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

Deprecated since version 4.20: This property will be removed in GTK 5. Use date instead.

props.month: int#

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

This property gets initially set to the current month.

Deprecated since version 4.20: This property will be removed in GTK 5. Use date instead.

props.show_day_names: bool#

Determines whether day names are displayed.

props.show_heading: bool#

Determines whether a heading is displayed.

props.show_week_numbers: bool#

Determines whether week numbers are displayed.

props.year: int#

The selected year.

This property gets initially set to the current year.

Deprecated since version 4.20: This property will be removed in GTK 5. Use date instead.

Signals#

class Calendar.signals
day_selected() None#

Emitted when the user selects a day.

next_month() None#

Emitted when the user switches to the next month.

next_year() None#

Emitted when user switches to the next year.

prev_month() None#

Emitted when the user switches to the previous month.

prev_year() None#

Emitted when user switches to the previous year.