Skip to content

Gtk.SearchBar

class — extends Widget, Accessible, Buildable, ConstraintTarget

Reveals a search entry when search is started.

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

It can also contain additional widgets, such as drop-down menus, or buttons. The search bar would appear when a search is started through typing on the keyboard, or the application’s search mode is toggled on.

For keyboard presses to start a search, the search bar must be told of a widget to capture key events from through SearchBar.set_key_capture_widget. This widget will typically be the top-level window, or a parent container of the search bar. Common shortcuts such as Ctrl+F should be handled as an application action, or through the menu items.

You will also need to tell the search bar about which entry you are using as your search entry using SearchBar.connect_entry.

The following example shows you how to create a more complex search entry.

A simple example

Shortcuts and Gestures

GtkSearchBar supports the following keyboard shortcuts:

  • <kbd>Escape</kbd> hides the search bar.

CSS nodes

searchbar
╰── revealer
    ╰── box
         ├── [child]
         ╰── [button.close]

GtkSearchBar has a main CSS node with name searchbar. It has a child node with name revealer that contains a node with name box. The box node contains both the CSS node of the child widget as well as an optional button node which gets the .close style class applied.

Accessibility

GtkSearchBar uses the AccessibleRole.search role.

Constructors

new

@classmethod
def new(cls) -> Widget

Creates a GtkSearchBar.

You will need to tell it about which widget is going to be your text entry using SearchBar.connect_entry.

Methods

connect_entry

def connect_entry(self, entry: Editable) -> None

Connects the GtkEditable widget passed as the one to be used in this search bar.

The entry should be a descendant of the search bar. Calling this function manually is only required if the entry isn’t the direct child of the search bar (as in our main example).

Parameters:

  • entry — a GtkEditable

get_child

def get_child(self) -> Widget | None

Gets the child widget of bar.

get_key_capture_widget

def get_key_capture_widget(self) -> Widget | None

Gets the widget that bar is capturing key events from.

get_search_mode

def get_search_mode(self) -> bool

Returns whether the search mode is on or off.

get_show_close_button

def get_show_close_button(self) -> bool

Returns whether the close button is shown.

set_child

def set_child(self, child: Widget | None = ...) -> None

Sets the child widget of bar.

Parameters:

  • child — the child widget

set_key_capture_widget

def set_key_capture_widget(self, widget: Widget | None = ...) -> None

Sets widget as the widget that bar will capture key events from.

If key events are handled by the search bar, the bar will be shown, and the entry populated with the entered text.

Note that despite the name of this function, the events are only 'captured' in the bubble phase, which means that editable child widgets of widget will receive text input before it gets captured. If that is not desired, you can capture and forward the events yourself with EventControllerKey.forward.

Parameters:

  • widget — a GtkWidget

set_search_mode

def set_search_mode(self, search_mode: bool) -> None

Switches the search mode on or off.

Parameters:

  • search_mode — the new state of the search mode

set_show_close_button

def set_show_close_button(self, visible: bool) -> None

Shows or hides the close button.

Applications that already have a “search” toggle button should not show a close button in their search bar, as it duplicates the role of the toggle button.

Parameters:

  • visible — whether the close button will be shown or not

Properties

child

child: Widget  # read/write

The child widget.

key_capture_widget

key_capture_widget: Widget  # read/write

The key capture widget.

search_mode_enabled

search_mode_enabled: bool  # read/write

Whether the search mode is on and the search bar shown.

show_close_button

show_close_button: bool  # read/write

Whether to show the close button in the search bar.