Gtk.Notebook

g GObject.GInterface GObject.GInterface Gtk.Accessible Gtk.Accessible GObject.GInterface->Gtk.Accessible Gtk.Buildable Gtk.Buildable GObject.GInterface->Gtk.Buildable Gtk.ConstraintTarget Gtk.ConstraintTarget GObject.GInterface->Gtk.ConstraintTarget GObject.InitiallyUnowned GObject.InitiallyUnowned Gtk.Widget Gtk.Widget GObject.InitiallyUnowned->Gtk.Widget GObject.Object GObject.Object GObject.Object->GObject.InitiallyUnowned Gtk.Accessible->Gtk.Widget Gtk.Buildable->Gtk.Widget Gtk.ConstraintTarget->Gtk.Widget Gtk.Notebook Gtk.Notebook Gtk.Widget->Gtk.Notebook

Example

../_images/Notebook.png
Subclasses:

None

Methods

Inherited:

Gtk.Widget (181), GObject.Object (37), Gtk.Accessible (15), Gtk.Buildable (1)

Structs:

Gtk.WidgetClass (18), GObject.ObjectClass (5)

class

new ()

append_page (child, tab_label)

append_page_menu (child, tab_label, menu_label)

detach_tab (child)

get_action_widget (pack_type)

get_current_page ()

get_group_name ()

get_menu_label (child)

get_menu_label_text (child)

get_n_pages ()

get_nth_page (page_num)

get_page (child)

get_pages ()

get_scrollable ()

get_show_border ()

get_show_tabs ()

get_tab_detachable (child)

get_tab_label (child)

get_tab_label_text (child)

get_tab_pos ()

get_tab_reorderable (child)

insert_page (child, tab_label, position)

insert_page_menu (child, tab_label, menu_label, position)

next_page ()

page_num (child)

popup_disable ()

popup_enable ()

prepend_page (child, tab_label)

prepend_page_menu (child, tab_label, menu_label)

prev_page ()

remove_page (page_num)

reorder_child (child, position)

set_action_widget (widget, pack_type)

set_current_page (page_num)

set_group_name (group_name)

set_menu_label (child, menu_label)

set_menu_label_text (child, menu_text)

set_scrollable (scrollable)

set_show_border (show_border)

set_show_tabs (show_tabs)

set_tab_detachable (child, detachable)

set_tab_label (child, tab_label)

set_tab_label_text (child, tab_text)

set_tab_pos (pos)

set_tab_reorderable (child, reorderable)

Virtual Methods

Inherited:

Gtk.Widget (25), GObject.Object (7), Gtk.Accessible (6), Gtk.Buildable (9)

Properties

Inherited:

Gtk.Widget (34), Gtk.Accessible (1)

Name

Type

Flags

Short Description

enable-popup

bool

r/w/en

group-name

str

r/w/en

page

int

r/w/en

pages

Gio.ListModel

r

scrollable

bool

r/w/en

show-border

bool

r/w/en

show-tabs

bool

r/w/en

tab-pos

Gtk.PositionType

r/w/en

Signals

Inherited:

Gtk.Widget (13), GObject.Object (1)

Name

Short Description

change-current-page

create-window

The ::create-window signal is emitted when a detachable tab is dropped on the root window.

focus-tab

move-focus-out

page-added

the ::page-added signal is emitted in the notebook right after a page is added to the notebook.

page-removed

the ::page-removed signal is emitted in the notebook right after a page is removed from the notebook.

page-reordered

the ::page-reordered signal is emitted in the notebook right after a page has been reordered.

reorder-tab

select-page

switch-page

Emitted when the user or a function changes the current page.

Fields

Inherited:

Gtk.Widget (13), GObject.Object (1)

Class Details

class Gtk.Notebook(**kwargs)
Bases:

Gtk.Widget

Abstract:

No

GtkNotebook is a container whose children are pages switched between using tabs.

An example Gtk.Notebook

There are many configuration options for GtkNotebook. Among other things, you can choose on which edge the tabs appear (see [method`Gtk`.Notebook.set_tab_pos]), whether, if there are too many tabs to fit the notebook should be made bigger or scrolling arrows added (see [method`Gtk`.Notebook.set_scrollable]), and whether there will be a popup menu allowing the users to switch pages. (see [method`Gtk`.Notebook.popup_enable]).

Gtk.Notebook as Gtk.Buildable

The GtkNotebook implementation of the GtkBuildable interface supports placing children into tabs by specifying “tab” as the “type” attribute of a <child> element. Note that the content of the tab must be created before the tab can be filled. A tab child can be specified without specifying a <child> type attribute.

To add a child widget in the notebooks action area, specify “action-start” or “action-end” as the “type” attribute of the <child> element.

An example of a UI definition fragment with GtkNotebook :

``xml <object class=”GtkNotebook”>

<child>
<object class=”GtkLabel” id=”notebook-content”>

<property name=”label”>Content</property>

</object>

</child> <child type=”tab”>

<object class=”GtkLabel” id=”notebook-tab”>

<property name=”label”>Tab</property>

</object>

</child>

</object> ``

CSS nodes

`` notebook ├── header.top │ ├── [<action widget>] │ ├── tabs │ │ ├── [arrow] │ │ ├── tab │ │ │ ╰── <tab label> ┊ ┊ ┊ │ │ ├── tab[.reorderable-page] │ │ │ ╰── <tab label> │ │ ╰── [arrow] │ ╰── [<action widget>] │ ╰── stack

├── <child> ┊ ╰── <child>

``

GtkNotebook has a main CSS node with name notebook, a subnode with name header and below that a subnode with name tabs which contains one subnode per tab with name tab.

If action widgets are present, their CSS nodes are placed next to the tabs node. If the notebook is scrollable, CSS nodes with name arrow are placed as first and last child of the tabs node.

The main node gets the .frame style class when the notebook has a border (see [method`Gtk`.Notebook.set_show_border]).

The header node gets one of the style class .top, .bottom, .left or .right, depending on where the tabs are placed. For reorderable pages, the tab node gets the .reorderable-page class.

A tab node gets the .dnd style class while it is moved with drag-and-drop.

The nodes are always arranged from left-to-right, regardless of text direction.

Accessibility

GtkNotebook uses the following roles:

classmethod new()[source]
Returns:

the newly created GtkNotebook

Return type:

Gtk.Widget

Creates a new GtkNotebook widget with no pages.

append_page(child, tab_label)[source]
Parameters:
  • child (Gtk.Widget) – the GtkWidget to use as the contents of the page

  • tab_label (Gtk.Widget or None) – the GtkWidget to be used as the label for the page, or None to use the default label, “page N”

Returns:

the index (starting from 0) of the appended page in the notebook, or -1 if function fails

Return type:

int

Appends a page to self.

append_page_menu(child, tab_label, menu_label)[source]
Parameters:
  • child (Gtk.Widget) – the GtkWidget to use as the contents of the page

  • tab_label (Gtk.Widget or None) – the GtkWidget to be used as the label for the page, or None to use the default label, “page N”

  • menu_label (Gtk.Widget or None) – the widget to use as a label for the page-switch menu, if that is enabled. If None, and tab_label is a GtkLabel or None, then the menu label will be a newly created label with the same text as tab_label; if tab_label is not a GtkLabel, menu_label must be specified if the page-switch menu is to be used.

Returns:

the index (starting from 0) of the appended page in the notebook, or -1 if function fails

Return type:

int

Appends a page to self, specifying the widget to use as the label in the popup menu.

detach_tab(child)[source]
Parameters:

child (Gtk.Widget) – a child

Removes the child from the notebook.

This function is very similar to [method`Gtk`.Notebook.remove_page], but additionally informs the notebook that the removal is happening as part of a tab DND operation, which should not be cancelled.

get_action_widget(pack_type)[source]
Parameters:

pack_type (Gtk.PackType) – pack type of the action widget to receive

Returns:

The action widget with the given pack_type or None when this action widget has not been set

Return type:

Gtk.Widget or None

Gets one of the action widgets.

See [method`Gtk`.Notebook.set_action_widget].

get_current_page()[source]
Returns:

the index (starting from 0) of the current page in the notebook. If the notebook has no pages, then -1 will be returned.

Return type:

int

Returns the page number of the current page.

get_group_name()[source]
Returns:

the group name, or None if none is set

Return type:

str or None

Gets the current group name for self.

get_menu_label(child)[source]
Parameters:

child (Gtk.Widget) – a widget contained in a page of self

Returns:

the menu label, or None if the notebook page does not have a menu label other than the default (the tab label).

Return type:

Gtk.Widget or None

Retrieves the menu label widget of the page containing child.

get_menu_label_text(child)[source]
Parameters:

child (Gtk.Widget) – the child widget of a page of the notebook.

Returns:

the text of the tab label, or None if the widget does not have a menu label other than the default menu label, or the menu label widget is not a GtkLabel. The string is owned by the widget and must not be freed.

Return type:

str or None

Retrieves the text of the menu label for the page containing child.

get_n_pages()[source]
Returns:

the number of pages in the notebook

Return type:

int

Gets the number of pages in a notebook.

get_nth_page(page_num)[source]
Parameters:

page_num (int) – the index of a page in the notebook, or -1 to get the last page

Returns:

the child widget, or None if page_num is out of bounds

Return type:

Gtk.Widget or None

Returns the child widget contained in page number page_num.

get_page(child)[source]
Parameters:

child (Gtk.Widget) – a child of self

Returns:

the GtkNotebookPage for child

Return type:

Gtk.NotebookPage

Returns the GtkNotebookPage for child.

get_pages()[source]
Returns:

a GListModel for the notebook’s children

Return type:

Gio.ListModel

Returns a GListModel that contains the pages of the notebook.

This can be used to keep an up-to-date view. The model also implements [iface`Gtk`.SelectionModel] and can be used to track and modify the visible page.

get_scrollable()[source]
Returns:

True if arrows for scrolling are present

Return type:

bool

Returns whether the tab label area has arrows for scrolling.

get_show_border()[source]
Returns:

True if the bevel is drawn

Return type:

bool

Returns whether a bevel will be drawn around the notebook pages.

get_show_tabs()[source]
Returns:

True if the tabs are shown

Return type:

bool

Returns whether the tabs of the notebook are shown.

get_tab_detachable(child)[source]
Parameters:

child (Gtk.Widget) – a child GtkWidget

Returns:

True if the tab is detachable.

Return type:

bool

Returns whether the tab contents can be detached from self.

get_tab_label(child)[source]
Parameters:

child (Gtk.Widget) – the page

Returns:

the tab label

Return type:

Gtk.Widget or None

Returns the tab label widget for the page child.

None is returned if child is not in self or if no tab label has specifically been set for child.

get_tab_label_text(child)[source]
Parameters:

child (Gtk.Widget) – a widget contained in a page of self

Returns:

the text of the tab label, or None if the tab label widget is not a GtkLabel. The string is owned by the widget and must not be freed.

Return type:

str or None

Retrieves the text of the tab label for the page containing child.

get_tab_pos()[source]
Returns:

the edge at which the tabs are drawn

Return type:

Gtk.PositionType

Gets the edge at which the tabs are drawn.

get_tab_reorderable(child)[source]
Parameters:

child (Gtk.Widget) – a child GtkWidget

Returns:

True if the tab is reorderable.

Return type:

bool

Gets whether the tab can be reordered via drag and drop or not.

insert_page(child, tab_label, position)[source]
Parameters:
  • child (Gtk.Widget) – the GtkWidget to use as the contents of the page

  • tab_label (Gtk.Widget or None) – the GtkWidget to be used as the label for the page, or None to use the default label, “page N”

  • position (int) – the index (starting at 0) at which to insert the page, or -1 to append the page after all other pages

Returns:

the index (starting from 0) of the inserted page in the notebook, or -1 if function fails

Return type:

int

Insert a page into self at the given position.

insert_page_menu(child, tab_label, menu_label, position)[source]
Parameters:
  • child (Gtk.Widget) – the GtkWidget to use as the contents of the page

  • tab_label (Gtk.Widget or None) – the GtkWidget to be used as the label for the page, or None to use the default label, “page N”

  • menu_label (Gtk.Widget or None) – the widget to use as a label for the page-switch menu, if that is enabled. If None, and tab_label is a GtkLabel or None, then the menu label will be a newly created label with the same text as tab_label; if tab_label is not a GtkLabel, menu_label must be specified if the page-switch menu is to be used.

  • position (int) – the index (starting at 0) at which to insert the page, or -1 to append the page after all other pages.

Returns:

the index (starting from 0) of the inserted page in the notebook

Return type:

int

Insert a page into self at the given position, specifying the widget to use as the label in the popup menu.

next_page()[source]

Switches to the next page.

Nothing happens if the current page is the last page.

page_num(child)[source]
Parameters:

child (Gtk.Widget) – a GtkWidget

Returns:

the index of the page containing child, or -1 if child is not in the notebook

Return type:

int

Finds the index of the page which contains the given child widget.

popup_disable()[source]

Disables the popup menu.

popup_enable()[source]

Enables the popup menu.

If the user clicks with the right mouse button on the tab labels, a menu with all the pages will be popped up.

prepend_page(child, tab_label)[source]
Parameters:
  • child (Gtk.Widget) – the GtkWidget to use as the contents of the page

  • tab_label (Gtk.Widget or None) – the GtkWidget to be used as the label for the page, or None to use the default label, “page N”

Returns:

the index (starting from 0) of the prepended page in the notebook, or -1 if function fails

Return type:

int

Prepends a page to self.

prepend_page_menu(child, tab_label, menu_label)[source]
Parameters:
  • child (Gtk.Widget) – the GtkWidget to use as the contents of the page

  • tab_label (Gtk.Widget or None) – the GtkWidget to be used as the label for the page, or None to use the default label, “page N”

  • menu_label (Gtk.Widget or None) – the widget to use as a label for the page-switch menu, if that is enabled. If None, and tab_label is a GtkLabel or None, then the menu label will be a newly created label with the same text as tab_label; if tab_label is not a GtkLabel, menu_label must be specified if the page-switch menu is to be used.

Returns:

the index (starting from 0) of the prepended page in the notebook, or -1 if function fails

Return type:

int

Prepends a page to self, specifying the widget to use as the label in the popup menu.

prev_page()[source]

Switches to the previous page.

Nothing happens if the current page is the first page.

remove_page(page_num)[source]
Parameters:

page_num (int) – the index of a notebook page, starting from 0. If -1, the last page will be removed.

Removes a page from the notebook given its index in the notebook.

reorder_child(child, position)[source]
Parameters:
  • child (Gtk.Widget) – the child to move

  • position (int) – the new position, or -1 to move to the end

Reorders the page containing child, so that it appears in position position.

If position is greater than or equal to the number of children in the list or negative, child will be moved to the end of the list.

set_action_widget(widget, pack_type)[source]
Parameters:

Sets widget as one of the action widgets.

Depending on the pack type the widget will be placed before or after the tabs. You can use a GtkBox if you need to pack more than one widget on the same side.

set_current_page(page_num)[source]
Parameters:

page_num (int) – index of the page to switch to, starting from 0. If negative, the last page will be used. If greater than the number of pages in the notebook, nothing will be done.

Switches to the page number page_num.

Note that due to historical reasons, Gtk.Notebook refuses to switch to a page unless the child widget is visible. Therefore, it is recommended to show child widgets before adding them to a notebook.

set_group_name(group_name)[source]
Parameters:

group_name (str or None) – the name of the notebook group, or None to unset it

Sets a group name for self.

Notebooks with the same name will be able to exchange tabs via drag and drop. A notebook with a None group name will not be able to exchange tabs with any other notebook.

set_menu_label(child, menu_label)[source]
Parameters:

Changes the menu label for the page containing child.

set_menu_label_text(child, menu_text)[source]
Parameters:
  • child (Gtk.Widget) – the child widget

  • menu_text (str) – the label text

Creates a new label and sets it as the menu label of child.

set_scrollable(scrollable)[source]
Parameters:

scrollable (bool) – True if scroll arrows should be added

Sets whether the tab label area will have arrows for scrolling if there are too many tabs to fit in the area.

set_show_border(show_border)[source]
Parameters:

show_border (bool) – True if a bevel should be drawn around the notebook

Sets whether a bevel will be drawn around the notebook pages.

This only has a visual effect when the tabs are not shown.

set_show_tabs(show_tabs)[source]
Parameters:

show_tabs (bool) – True if the tabs should be shown

Sets whether to show the tabs for the notebook or not.

set_tab_detachable(child, detachable)[source]
Parameters:
  • child (Gtk.Widget) – a child GtkWidget

  • detachable (bool) – whether the tab is detachable or not

Sets whether the tab can be detached from self to another notebook or widget.

Note that two notebooks must share a common group identifier (see [method`Gtk`.Notebook.set_group_name]) to allow automatic tabs interchange between them.

If you want a widget to interact with a notebook through DnD (i.e.: accept dragged tabs from it) it must be set as a drop destination and accept the target “GTK_NOTEBOOK_TAB”. The notebook will fill the selection with a Gtk.Widget pointing to the child widget that corresponds to the dropped tab.

Note that you should use [method`Gtk`.Notebook.detach_tab] instead of [method`Gtk`.Notebook.remove_page] if you want to remove the tab from the source notebook as part of accepting a drop. Otherwise, the source notebook will think that the dragged tab was removed from underneath the ongoing drag operation, and will initiate a drag cancel animation.

```c static void on_drag_data_received (Gtk.Widget *widget, Gdk.Drop *drop, GtkSelectionData *data, int time, object user_data) { GtkDrag *drag; Gtk.Widget *notebook; Gtk.Widget **child;

drag = gtk_drop_get_drag (drop); notebook = GObject.Object.get_data (drag, “gtk-notebook-drag-origin”); child = (void*) gtk_selection_data_get_data (data);

// process_widget (*child);

Gtk.Notebook.detach_tab (GTK_NOTEBOOK (notebook), *child); } ```

If you want a notebook to accept drags from other widgets, you will have to set your own DnD code to do it.

set_tab_label(child, tab_label)[source]
Parameters:

Changes the tab label for child.

If None is specified for tab_label, then the page will have the label “page N”.

set_tab_label_text(child, tab_text)[source]
Parameters:
  • child (Gtk.Widget) – the page

  • tab_text (str) – the label text

Creates a new label and sets it as the tab label for the page containing child.

set_tab_pos(pos)[source]
Parameters:

pos (Gtk.PositionType) – the edge to draw the tabs at

Sets the edge at which the tabs are drawn.

set_tab_reorderable(child, reorderable)[source]
Parameters:
  • child (Gtk.Widget) – a child GtkWidget

  • reorderable (bool) – whether the tab is reorderable or not

Sets whether the notebook tab can be reordered via drag and drop or not.

Signal Details

Gtk.Notebook.signals.change_current_page(notebook, object)
Signal Name:

change-current-page

Flags:

RUN_LAST, ACTION

Parameters:
  • notebook (Gtk.Notebook) – The object which received the signal

  • object (int) –

Return type:

bool

Gtk.Notebook.signals.create_window(notebook, page)
Signal Name:

create-window

Flags:

RUN_LAST

Parameters:
  • notebook (Gtk.Notebook) – The object which received the signal

  • page (Gtk.Widget) – the tab of notebook that is being detached

Returns:

a GtkNotebook that page should be added to

Return type:

Gtk.Notebook or None

The ::create-window signal is emitted when a detachable tab is dropped on the root window.

A handler for this signal can create a window containing a notebook where the tab will be attached. It is also responsible for moving/resizing the window and adding the necessary properties to the notebook (e.g. the GtkNotebook :group-name ).

Gtk.Notebook.signals.focus_tab(notebook, object)
Signal Name:

focus-tab

Flags:

RUN_LAST, ACTION

Parameters:
Return type:

bool

Gtk.Notebook.signals.move_focus_out(notebook, object)
Signal Name:

move-focus-out

Flags:

RUN_LAST, ACTION

Parameters:
Gtk.Notebook.signals.page_added(notebook, child, page_num)
Signal Name:

page-added

Flags:

RUN_LAST

Parameters:
  • notebook (Gtk.Notebook) – The object which received the signal

  • child (Gtk.Widget) – the child GtkWidget affected

  • page_num (int) – the new page number for child

the ::page-added signal is emitted in the notebook right after a page is added to the notebook.

Gtk.Notebook.signals.page_removed(notebook, child, page_num)
Signal Name:

page-removed

Flags:

RUN_LAST

Parameters:
  • notebook (Gtk.Notebook) – The object which received the signal

  • child (Gtk.Widget) – the child GtkWidget affected

  • page_num (int) – the child page number

the ::page-removed signal is emitted in the notebook right after a page is removed from the notebook.

Gtk.Notebook.signals.page_reordered(notebook, child, page_num)
Signal Name:

page-reordered

Flags:

RUN_LAST

Parameters:
  • notebook (Gtk.Notebook) – The object which received the signal

  • child (Gtk.Widget) – the child GtkWidget affected

  • page_num (int) – the new page number for child

the ::page-reordered signal is emitted in the notebook right after a page has been reordered.

Gtk.Notebook.signals.reorder_tab(notebook, object, p0)
Signal Name:

reorder-tab

Flags:

RUN_LAST, ACTION

Parameters:
Return type:

bool

Gtk.Notebook.signals.select_page(notebook, object)
Signal Name:

select-page

Flags:

RUN_LAST, ACTION

Parameters:
  • notebook (Gtk.Notebook) – The object which received the signal

  • object (bool) –

Return type:

bool

Gtk.Notebook.signals.switch_page(notebook, page, page_num)
Signal Name:

switch-page

Flags:

RUN_LAST

Parameters:
  • notebook (Gtk.Notebook) – The object which received the signal

  • page (Gtk.Widget) – the new current page

  • page_num (int) – the index of the page

Emitted when the user or a function changes the current page.

Property Details

Gtk.Notebook.props.enable_popup
Name:

enable-popup

Type:

bool

Default Value:

False

Flags:

READABLE, WRITABLE, EXPLICIT_NOTIFY

If True, pressing the right mouse button on the notebook shows a page switching menu.

Gtk.Notebook.props.group_name
Name:

group-name

Type:

str

Default Value:

None

Flags:

READABLE, WRITABLE, EXPLICIT_NOTIFY

Group name for tab drag and drop.

Gtk.Notebook.props.page
Name:

page

Type:

int

Default Value:

-1

Flags:

READABLE, WRITABLE, EXPLICIT_NOTIFY

The index of the current page.

Gtk.Notebook.props.pages
Name:

pages

Type:

Gio.ListModel

Default Value:

None

Flags:

READABLE

A selection model with the pages.

Gtk.Notebook.props.scrollable
Name:

scrollable

Type:

bool

Default Value:

False

Flags:

READABLE, WRITABLE, EXPLICIT_NOTIFY

If True, scroll arrows are added if there are too many pages to fit.

Gtk.Notebook.props.show_border
Name:

show-border

Type:

bool

Default Value:

True

Flags:

READABLE, WRITABLE, EXPLICIT_NOTIFY

Whether the border should be shown.

Gtk.Notebook.props.show_tabs
Name:

show-tabs

Type:

bool

Default Value:

True

Flags:

READABLE, WRITABLE, EXPLICIT_NOTIFY

Whether tabs should be shown.

Gtk.Notebook.props.tab_pos
Name:

tab-pos

Type:

Gtk.PositionType

Default Value:

Gtk.PositionType.TOP

Flags:

READABLE, WRITABLE, EXPLICIT_NOTIFY

Which side of the notebook holds the tabs.