• Gtk 4.0 »
  • Classes »
  • Gtk.SearchBar
  • Gtk.SearchBar
    • Example
    • Methods
    • Virtual Methods
    • Properties
    • Signals
    • Fields
    • Class Details
      • Gtk.SearchBar
        • Gtk.SearchBar.new()
        • Gtk.SearchBar.connect_entry()
        • Gtk.SearchBar.get_child()
        • Gtk.SearchBar.get_key_capture_widget()
        • Gtk.SearchBar.get_search_mode()
        • Gtk.SearchBar.get_show_close_button()
        • Gtk.SearchBar.set_child()
        • Gtk.SearchBar.set_key_capture_widget()
        • Gtk.SearchBar.set_search_mode()
        • Gtk.SearchBar.set_show_close_button()
    • Property Details
      • Gtk.SearchBar.props.child
      • Gtk.SearchBar.props.key_capture_widget
      • Gtk.SearchBar.props.search_mode_enabled
      • Gtk.SearchBar.props.show_close_button
  • Gtk 4.0 »
  • Classes »
  • Gtk.SearchBar
  • Gtk.SearchBar
    • Example
    • Methods
    • Virtual Methods
    • Properties
    • Signals
    • Fields
    • Class Details
      • Gtk.SearchBar
        • Gtk.SearchBar.new()
        • Gtk.SearchBar.connect_entry()
        • Gtk.SearchBar.get_child()
        • Gtk.SearchBar.get_key_capture_widget()
        • Gtk.SearchBar.get_search_mode()
        • Gtk.SearchBar.get_show_close_button()
        • Gtk.SearchBar.set_child()
        • Gtk.SearchBar.set_key_capture_widget()
        • Gtk.SearchBar.set_search_mode()
        • Gtk.SearchBar.set_show_close_button()
    • Property Details
      • Gtk.SearchBar.props.child
      • Gtk.SearchBar.props.key_capture_widget
      • Gtk.SearchBar.props.search_mode_enabled
      • Gtk.SearchBar.props.show_close_button

Gtk.SearchBar¶

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.SearchBar Gtk.SearchBar Gtk.Widget->Gtk.SearchBar

Example¶

../_images/SearchBar.png
Subclasses:

None

Methods¶

Inherited:

Gtk.Widget (183), GObject.Object (37), Gtk.Accessible (17), Gtk.Buildable (1)

Structs:

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

class

new ()

connect_entry (entry)

get_child ()

get_key_capture_widget ()

get_search_mode ()

get_show_close_button ()

set_child (child)

set_key_capture_widget (widget)

set_search_mode (search_mode)

set_show_close_button (visible)

Virtual Methods¶

Inherited:

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

Properties¶

Inherited:

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

Name

Type

Flags

Short Description

child

Gtk.Widget

r/w/c/en

key-capture-widget

Gtk.Widget

r/w/c/en

search-mode-enabled

bool

r/w/en

show-close-button

bool

r/w/c/en

Signals¶

Inherited:

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

Fields¶

Inherited:

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

Class Details¶

class Gtk.SearchBar(**kwargs)¶
Bases:

Gtk.Widget

Abstract:

No

Reveals a search entry when search is started.

<picture> <source srcset=”search-bar-dark.png” media=”(prefers-color-scheme: dark)”> <img alt=”An example Gtk.SearchBar" 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 [method`Gtk`.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 [method`Gtk`.SearchBar.connect_entry].

Creating a search bar

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 [enum`Gtk`.AccessibleRole.search] role.

classmethod new()[source]¶
Returns:

a new GtkSearchBar

Return type:

Gtk.Widget

Creates a GtkSearchBar.

You will need to tell it about which widget is going to be your text entry using [method`Gtk`.SearchBar.connect_entry].

connect_entry(entry)[source]¶
Parameters:

entry (Gtk.Editable) – a GtkEditable

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).

get_child()[source]¶
Returns:

the child widget of self

Return type:

Gtk.Widget or None

Gets the child widget of self.

get_key_capture_widget()[source]¶
Returns:

The key capture widget.

Return type:

Gtk.Widget or None

Gets the widget that self is capturing key events from.

get_search_mode()[source]¶
Returns:

whether search mode is toggled on

Return type:

bool

Returns whether the search mode is on or off.

get_show_close_button()[source]¶
Returns:

whether the close button is shown

Return type:

bool

Returns whether the close button is shown.

set_child(child)[source]¶
Parameters:

child (Gtk.Widget or None) – the child widget

Sets the child widget of self.

set_key_capture_widget(widget)[source]¶
Parameters:

widget (Gtk.Widget or None) – a GtkWidget

Sets widget as the widget that self 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 [method`Gtk`.EventControllerKey.forward].

set_search_mode(search_mode)[source]¶
Parameters:

search_mode (bool) – the new state of the search mode

Switches the search mode on or off.

set_show_close_button(visible)[source]¶
Parameters:

visible (bool) – whether the close button will be shown or not

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.

Property Details¶

Gtk.SearchBar.props.child¶
Name:

child

Type:

Gtk.Widget

Default Value:

None

Flags:

READABLE, WRITABLE, CONSTRUCT, EXPLICIT_NOTIFY

The child widget.

Gtk.SearchBar.props.key_capture_widget¶
Name:

key-capture-widget

Type:

Gtk.Widget

Default Value:

None

Flags:

READABLE, WRITABLE, CONSTRUCT, EXPLICIT_NOTIFY

The key capture widget.

Gtk.SearchBar.props.search_mode_enabled¶
Name:

search-mode-enabled

Type:

bool

Default Value:

False

Flags:

READABLE, WRITABLE, EXPLICIT_NOTIFY

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

Gtk.SearchBar.props.show_close_button¶
Name:

show-close-button

Type:

bool

Default Value:

False

Flags:

READABLE, WRITABLE, CONSTRUCT, EXPLICIT_NOTIFY

Whether to show the close button in the search bar.