Skip to content

Gtk.WidgetClass

record (struct)

Methods

add_shortcut

def add_shortcut(self, shortcut: Shortcut) -> None

Installs a shortcut in widget_class.

Every instance created for widget_class or its subclasses will inherit this shortcut and trigger it.

Shortcuts added this way will be triggered in the PropagationPhase.bubble phase, which means they may also trigger if child widgets have focus.

This function must only be used in class initialization functions otherwise it is not guaranteed that the shortcut will be installed.

Parameters:

  • shortcut — the shortcut to add

bind_template_callback_full

def bind_template_callback_full(self, callback_name: str, callback_symbol: GObject.Callback) -> None

Associates a name to be used in GtkBuilder XML with a symbol.

This function is not supported after WidgetClass.set_template_scope has been used on widget_class.

See BuilderCScope.add_callback_symbol.

Note that this must be called from a composite widget classes class initializer after calling WidgetClass.set_template.

Parameters:

  • callback_name — name of the callback as expected in the template XML
  • callback_symbol — the callback symbol

bind_template_child_full

def bind_template_child_full(self, name: str, internal_child: bool, struct_offset: int) -> None

Assigns an object declared in the class template XML to be set to a location on a freshly built instance’s private data, or alternatively accessible via Widget.get_template_child.

The struct can point either into the public instance, then you should use G_STRUCT_OFFSET(WidgetType, member) for struct_offset, or in the private struct, then you should use G_PRIVATE_OFFSET(WidgetType, member).

An explicit strong reference will be held automatically for the duration of your instance’s life cycle, it will be released automatically when GObjectClass.dispose() runs on your instance and if a nonzero struct_offset is specified, then the automatic location in your instance public or private data will be set to NULL. You can however access an automated child pointer the first time your classes GObjectClass.dispose() runs, or alternatively in Widget.destroy.

If internal_child is specified, Buildable.get_internal_child will be automatically implemented by the widget class so there is no need to implement it manually.

The wrapper macros Gtk.widget_class_bind_template_child, Gtk.widget_class_bind_template_child_internal, Gtk.widget_class_bind_template_child_private and Gtk.widget_class_bind_template_child_internal_private might be more convenient to use.

Note that this must be called from a composite widget classes class initializer after calling WidgetClass.set_template.

Parameters:

  • name — ID of the child defined in the template XML
  • internal_child — whether the child should be accessible as an “internal-child” when this class is used in GtkBuilder XML
  • struct_offset — The offset into the composite widget’s instance public or private structure where the automated child pointer should be set, or 0 to not assign the pointer

get_accessible_role

def get_accessible_role(self) -> AccessibleRole

Retrieves the accessible role used by the given widget class.

Different accessible roles have different states, and are rendered differently by assistive technologies.

See also: Accessible.get_accessible_role.

get_activate_signal

def get_activate_signal(self) -> int

Retrieves the signal id for the activation signal.

The activation signal is set using WidgetClass.set_activate_signal.

get_css_name

def get_css_name(self) -> str

Gets the name used by this class for matching in CSS code.

See WidgetClass.set_css_name for details.

get_layout_manager_type

def get_layout_manager_type(self) -> type | GObject.Type

Retrieves the type of the LayoutManager used by widgets of class widget_class.

See also: WidgetClass.set_layout_manager_type.

install_action

def install_action(self, action_name: str, parameter_type: str | None, activate: WidgetActionActivateFunc) -> None

Adds an action for all instances of a widget class.

This function should be called at class initialization time.

Actions installed by this function are stateless. The only state they have is whether they are enabled or not (which can be changed with Widget.action_set_enabled).

Parameters:

  • action_name — a prefixed action name, such as "clipboard.paste"
  • parameter_type — the parameter type
  • activate — callback to use when the action is activated

install_property_action

def install_property_action(self, action_name: str, property_name: str) -> None

Installs an action called action_name on widget_class and binds its state to the value of the property_name property.

This function will perform a few sanity checks on the property selected via property_name. Namely, the property must exist, must be readable, writable and must not be construct-only. There are also restrictions on the type of the given property, it must be boolean, int, unsigned int, double or string. If any of these conditions are not met, a critical warning will be printed and no action will be added.

The state type of the action matches the property type.

If the property is boolean, the action will have no parameter and toggle the property value. Otherwise, the action will have a parameter of the same type as the property.

Parameters:

  • action_name — name of the action
  • property_name — name of a property in instances of widget_class or any parent class

query_action

def query_action(self, index_: int) -> tuple[bool, type | GObject.Type, str, GLib.VariantType, str]

Returns details about an action that has been installed for widget_class.

See WidgetClass.install_action for details on how to install actions.

Note that this function will also return actions defined by parent classes. You can identify those by looking at owner.

Parameters:

  • index_ — position of the action to query

set_accessible_role

def set_accessible_role(self, accessible_role: AccessibleRole | int) -> None

Sets the accessible role used by the given widget class.

Different accessible roles have different states, and are rendered differently by assistive technologies.

Parameters:

  • accessible_role — the accessible role to use

set_activate_signal

def set_activate_signal(self, signal_id: int) -> None

Sets the activation signal for a widget class.

The signal will be emitted when calling Widget.activate.

The signal_id must have been registered with [function.GObject.signal_new] or GObject.signal_newv before calling this function.

Parameters:

  • signal_id — the id for the activate signal

set_activate_signal_from_name

def set_activate_signal_from_name(self, signal_name: str) -> None

Sets the activation signal for a widget class.

The signal id will by looked up by signal_name.

The signal will be emitted when calling Widget.activate.

The signal_name must have been registered with [function.GObject.signal_new] or GObject.signal_newv before calling this function.

Parameters:

  • signal_name — the name of the activate signal of widget_type

set_css_name

def set_css_name(self, name: str) -> None

Sets the name to be used for CSS matching of widgets.

If this function is not called for a given class, the name set on the parent class is used. By default, GtkWidget uses the name "widget".

Parameters:

  • name — name to use

set_layout_manager_type

def set_layout_manager_type(self, type: type | GObject.Type) -> None

Sets the type to be used for creating layout managers for widgets of widget_class.

The given type must be a subtype of LayoutManager.

This function should only be called from class init functions of widgets.

Parameters:

  • type — the object type that implements the GtkLayoutManager for widget_class

set_template

def set_template(self, template_bytes: bytes) -> None

This should be called at class initialization time to specify the GtkBuilder XML to be used to extend a widget.

For convenience, WidgetClass.set_template_from_resource is also provided.

Note that any class that installs templates must call Widget.init_template in the widget’s instance initializer.

Parameters:

  • template_bytesGBytes holding the GtkBuilder XML

set_template_from_resource

def set_template_from_resource(self, resource_name: str) -> None

A convenience function that calls WidgetClass.set_template with the contents of a resource.

Note that any class that installs templates must call Widget.init_template in the widget’s instance initializer.

Parameters:

  • resource_name — resource path to load the template from

set_template_scope

def set_template_scope(self, scope: BuilderScope) -> None

Overrides the default scope to be used when parsing the class template.

This function is intended for language bindings.

Note that this must be called from a composite widget classes class initializer after calling WidgetClass.set_template.

Parameters:

  • scopeGtkBuilderScope to use when loading the class template

Properties

parent_class

parent_class: GObject.InitiallyUnownedClass  # read/write

show

show: Any  # read/write

hide

hide: Any  # read/write

map

map: Any  # read/write

unmap

unmap: Any  # read/write

realize

realize: Any  # read/write

unrealize

unrealize: Any  # read/write

root

root: Any  # read/write

unroot

unroot: Any  # read/write

size_allocate

size_allocate: Any  # read/write

state_flags_changed

state_flags_changed: Any  # read/write

direction_changed

direction_changed: Any  # read/write

get_request_mode

get_request_mode: Any  # read/write

measure

measure: Any  # read/write

mnemonic_activate

mnemonic_activate: Any  # read/write

grab_focus

grab_focus: Any  # read/write

focus

focus: Any  # read/write

set_focus_child

set_focus_child: Any  # read/write

move_focus

move_focus: Any  # read/write

keynav_failed

keynav_failed: Any  # read/write

query_tooltip

query_tooltip: Any  # read/write

compute_expand

compute_expand: Any  # read/write

css_changed

css_changed: Any  # read/write

system_setting_changed

system_setting_changed: Any  # read/write

snapshot

snapshot: Any  # read/write

contains

contains: Any  # read/write

priv

priv: WidgetClassPrivate  # read/write

padding

padding: list[int]  # read/write