Gtk.Notebook¶
class — extends Widget, Accessible, Buildable, ConstraintTarget
Switches between children using tabs.
<picture> <source srcset="notebook-dark.png" media="(prefers-color-scheme: dark)"> <img alt="An example GtkNotebook" src="notebook.png"> </picture>
There are many configuration options for GtkNotebook. Among
other things, you can choose on which edge the tabs appear
(see Notebook.set_tab_pos), whether, if there are
too many tabs to fit the notebook should be made bigger or scrolling
arrows added (see Notebook.set_scrollable), and whether
there will be a popup menu allowing the users to switch pages.
(see Notebook.popup_enable).
GtkNotebook as GtkBuildable¶
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:
<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>
Shortcuts and Gestures¶
GtkNotebook supports the following keyboard shortcuts:
- <kbd>Shift</kbd>+<kbd>F10</kbd> or <kbd>Menu</kbd> opens the context menu.
- <kbd>Home</kbd> moves the focus to the first tab.
- <kbd>End</kbd> moves the focus to the last tab.
Additionally, the following signals have default keybindings:
Notebook.change-current-pageNotebook.focus-tabNotebook.move-focus-outNotebook.reorder-tabNotebook.select-page
Tabs support drag-and-drop between notebooks sharing the same group-name,
or to new windows by handling the ::create-window signal.
Actions¶
GtkNotebook defines a set of built-in actions:
menu.popupopens the tabs context menu.
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 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:
AccessibleRole.groupfor the notebook widgetAccessibleRole.tab_listfor the list of tabsAccessibleRole.tabrole for each tabAccessibleRole.tab_panelfor each page
Constructors¶
new¶
Creates a new GtkNotebook widget with no pages.
Methods¶
append_page¶
Appends a page to notebook.
Parameters:
child— theGtkWidgetto use as the contents of the pagetab_label— theGtkWidgetto be used as the label for the page, orNoneto use the default label, “page N”
append_page_menu¶
def append_page_menu(self, child: Widget, tab_label: Widget | None = ..., menu_label: Widget | None = ...) -> int
Appends a page to notebook, specifying the widget to use as the
label in the popup menu.
Parameters:
child— theGtkWidgetto use as the contents of the pagetab_label— theGtkWidgetto be used as the label for the page, orNoneto use the default label, “page N”menu_label— the widget to use as a label for the page-switch menu, if that is enabled. IfNone, andtab_labelis aGtkLabelorNone, then the menu label will be a newly created label with the same text astab_label; iftab_labelis not aGtkLabel,menu_labelmust be specified if the page-switch menu is to be used.
detach_tab¶
Removes the child from the notebook.
This function is very similar to 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.
Parameters:
child— a child
get_action_widget¶
Gets one of the action widgets.
See Notebook.set_action_widget.
Parameters:
pack_type— pack type of the action widget to receive
get_current_page¶
Returns the page number of the current page.
get_group_name¶
Gets the current group name for notebook.
get_menu_label¶
Retrieves the menu label widget of the page containing child.
Parameters:
child— a widget contained in a page ofnotebook
get_menu_label_text¶
Retrieves the text of the menu label for the page containing
child.
Parameters:
child— the child widget of a page of the notebook.
get_n_pages¶
Gets the number of pages in a notebook.
get_nth_page¶
Returns the child widget contained in page number page_num.
Parameters:
page_num— the index of a page in the notebook, or -1 to get the last page
get_page¶
Returns the GtkNotebookPage for child.
Parameters:
child— a child ofnotebook
get_pages¶
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 SelectionModel and can be used to track
and modify the visible page.
get_scrollable¶
Returns whether the tab label area has arrows for scrolling.
get_show_border¶
Returns whether a bevel will be drawn around the notebook pages.
get_show_tabs¶
Returns whether the tabs of the notebook are shown.
get_tab_detachable¶
Returns whether the tab contents can be detached from notebook.
Parameters:
child— a childGtkWidget
get_tab_label¶
Returns the tab label widget for the page child.
None is returned if child is not in notebook or
if no tab label has specifically been set for child.
Parameters:
child— the page
get_tab_label_text¶
Retrieves the text of the tab label for the page containing
child.
Parameters:
child— a widget contained in a page ofnotebook
get_tab_pos¶
Gets the edge at which the tabs are drawn.
get_tab_reorderable¶
Gets whether the tab can be reordered via drag and drop or not.
Parameters:
child— a childGtkWidget
insert_page¶
Insert a page into notebook at the given position.
Parameters:
child— theGtkWidgetto use as the contents of the pagetab_label— theGtkWidgetto be used as the label for the page, orNoneto use the default label, “page N”position— the index (starting at 0) at which to insert the page, or -1 to append the page after all other pages
insert_page_menu¶
def insert_page_menu(self, child: Widget, tab_label: Widget | None, menu_label: Widget | None, position: int) -> int
Insert a page into notebook at the given position, specifying
the widget to use as the label in the popup menu.
Parameters:
child— theGtkWidgetto use as the contents of the pagetab_label— theGtkWidgetto be used as the label for the page, orNoneto use the default label, “page N”menu_label— the widget to use as a label for the page-switch menu, if that is enabled. IfNone, andtab_labelis aGtkLabelorNone, then the menu label will be a newly created label with the same text astab_label; iftab_labelis not aGtkLabel,menu_labelmust be specified if the page-switch menu is to be used.position— the index (starting at 0) at which to insert the page, or -1 to append the page after all other pages.
next_page¶
Switches to the next page.
Nothing happens if the current page is the last page.
page_num¶
Finds the index of the page which contains the given child widget.
Parameters:
child— aGtkWidget
popup_disable¶
Disables the popup menu.
popup_enable¶
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¶
Prepends a page to notebook.
Parameters:
child— theGtkWidgetto use as the contents of the pagetab_label— theGtkWidgetto be used as the label for the page, orNoneto use the default label, “page N”
prepend_page_menu¶
def prepend_page_menu(self, child: Widget, tab_label: Widget | None = ..., menu_label: Widget | None = ...) -> int
Prepends a page to notebook, specifying the widget to use as the
label in the popup menu.
Parameters:
child— theGtkWidgetto use as the contents of the pagetab_label— theGtkWidgetto be used as the label for the page, orNoneto use the default label, “page N”menu_label— the widget to use as a label for the page-switch menu, if that is enabled. IfNone, andtab_labelis aGtkLabelorNone, then the menu label will be a newly created label with the same text astab_label; iftab_labelis not aGtkLabel,menu_labelmust be specified if the page-switch menu is to be used.
prev_page¶
Switches to the previous page.
Nothing happens if the current page is the first page.
remove_page¶
Removes a page from the notebook given its index in the notebook.
Parameters:
page_num— the index of a notebook page, starting from 0. If -1, the last page will be removed.
reorder_child¶
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.
Parameters:
child— the child to moveposition— the new position, or -1 to move to the end
set_action_widget¶
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.
Parameters:
widget— aGtkWidgetpack_type— pack type of the action widget
set_current_page¶
Switches to the page number page_num.
Note that due to historical reasons, GtkNotebook 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.
Parameters:
page_num— 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.
set_group_name¶
Sets a group name for notebook.
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.
Parameters:
group_name— the name of the notebook group, orNoneto unset it
set_menu_label¶
Changes the menu label for the page containing child.
Parameters:
child— the child widgetmenu_label— the menu label, orNonefor default
set_menu_label_text¶
Creates a new label and sets it as the menu label of child.
Parameters:
child— the child widgetmenu_text— the label text
set_scrollable¶
Sets whether the tab label area will have arrows for scrolling if there are too many tabs to fit in the area.
Parameters:
scrollable—Trueif scroll arrows should be added
set_show_border¶
Sets whether a bevel will be drawn around the notebook pages.
This only has a visual effect when the tabs are not shown.
Parameters:
show_border—Trueif a bevel should be drawn around the notebook
set_show_tabs¶
Sets whether to show the tabs for the notebook or not.
Parameters:
show_tabs—Trueif the tabs should be shown
set_tab_detachable¶
Sets whether the tab can be detached from notebook to another
notebook or widget.
Note that two notebooks must share a common group identifier
(see 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 by adding to it a DropTarget controller that accepts
the GType GTK_TYPE_NOTEBOOK_PAGE. The :value of said drop target will be
preloaded with a NotebookPage object that corresponds to the
dropped tab, so you can process the value via ::accept or ::drop signals.
Note that you should use Notebook.detach_tab instead
of 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.
static void
on_drag_data_received (GtkWidget *widget,
GdkDrop *drop,
GtkSelectionData *data,
guint time,
gpointer user_data)
{
GtkDrag *drag;
GtkWidget *notebook;
GtkWidget **child;
drag = gtk_drop_get_drag (drop);
notebook = g_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.
Parameters:
child— a childGtkWidgetdetachable— whether the tab is detachable or not
set_tab_label¶
Changes the tab label for child.
If None is specified for tab_label, then the page will
have the label “page N”.
Parameters:
child— the pagetab_label— the tab label widget to use, orNonefor default tab label
set_tab_label_text¶
Creates a new label and sets it as the tab label for the page
containing child.
Parameters:
child— the pagetab_text— the label text
set_tab_pos¶
Sets the edge at which the tabs are drawn.
Parameters:
pos— the edge to draw the tabs at
set_tab_reorderable¶
Sets whether the notebook tab can be reordered via drag and drop or not.
Parameters:
child— a childGtkWidgetreorderable— whether the tab is reorderable or not
Properties¶
enable_popup¶
If True, pressing the right mouse button on the notebook shows a page switching menu.
group_name¶
Group name for tab drag and drop.
page¶
The index of the current page.
pages¶
A selection model with the pages.
scrollable¶
If True, scroll arrows are added if there are too many pages to fit.
show_border¶
Whether the border should be shown.
show_tabs¶
Whether tabs should be shown.
tab_pos¶
Which side of the notebook holds the tabs.
Signals¶
change-current-page¶
Emitted when the current page should be changed.
The default bindings for this signal are <kbd>Ctrl</kbd>+<kbd>Alt</kbd>+<kbd>PgUp</kbd>, <kbd>Ctrl</kbd>+<kbd>Alt</kbd>+<kbd>PgDn</kbd>, <kbd>Ctrl</kbd>+<kbd>PgUp</kbd> and <kbd>Ctrl</kbd>+<kbd>PgDn</kbd>.
create-window¶
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 ).
focus-tab¶
Emitted when a tab should be focused.
move-focus-out¶
Emitted when focus was moved out.
The default bindings for this signal are <kbd>Ctrl</kbd>+<kbd>Tab</kbd>, <kbd>Ctrl</kbd>+<kbd>Shift</kbd>+<kbd>Tab</kbd>, <kbd>Ctrl</kbd>+<kbd>←</kbd>, <kbd>Ctrl</kbd>+<kbd>→</kbd>, <kbd>Ctrl</kbd>+<kbd>↑</kbd> and <kbd>Ctrl</kbd>+<kbd>↓</kbd>.
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¶
Emitted when the tab should be reordered.
The default bindings for this signal are <kbd>Alt</kbd>+<kbd>Home</kbd>, <kbd>Alt</kbd>+<kbd>End</kbd>, <kbd>Alt</kbd>+<kbd>PgUp</kbd>, <kbd>Alt</kbd>+<kbd>PgDn</kbd>, <kbd>Alt</kbd>+<kbd>←</kbd>, <kbd>Alt</kbd>+<kbd>→</kbd>, <kbd>Alt</kbd>+<kbd>↑</kbd> and <kbd>Alt</kbd>+<kbd>↓</kbd>.
select-page¶
Emitted when a page should be selected.
The default binding for this signal is <kbd>␣</kbd>.
switch-page¶
Emitted when the user or a function changes the current page.