Is there a way to switch focus between windows in a single GTK application?

Viewed 1695

I'll write details in python b/c that's what I'm using but the same question should apply in C++.

Suppose I have a Gtk.Application instance that has two separate Gtk.Window objects attached. These are both displayed side by side when using the application. Let's call them window A and B

I want to trigger the application to switch focus between window A and window B, but I can't find a way to do it.

  • The method: Gtk.Window.set_focus(widget) will switch the focus between widgets within the same window.

  • The method: Gtk.Widget.grab_focus() does the same thing only it automatically targets the calling instance.

  • The method: Gtk.Application.get_windows() returns a list of application windows in the order of most recent focus.

When I call windowA.grab_focus() while B is focused, it does nothing. Same effect for calling windowA.set_focus(windowA)

It seems like GTK only has the power to control intra-window focus changes and not focus changes between different windows. This could make sense as I can see why this functionality would be delegated to the window manager. But it still seems like there should be a way to do this.

1 Answers

You probably want GtkWindow.present(). This will focus the window and raise it to the top. It will automatically focus the default widget.

Related