3

Currently I'm working on a pyGTK3 application for which I want to set the focus chain. When using the code below (simplified version) something interesting happens GTK focuses correctly on the first button but on tab nothing happens.

At first I thought this was caused by GTK not knowing the chain, but then I tried shift+tab which resulted in the last element of the chain get the focus which is correct. Yet another shift+tab doesn't move the focus away from the last button.

So I figured that their is something wrong with my code, do I need to update the chain order by hand? Or I am missing something?

class Screen(Gtk.Grid):
  def __init__(self, parent, core, video_widget):
    Gtk.Grid.__init__(self)

    # adds the control buttons
    self.controls = load_controls(self)

    self.set_focus_chain((self.controls.play, self.controls.backward, self.controls.recording))
B8vrede
  • 4,432
  • 3
  • 27
  • 40
  • 1
    Shouldn't you set the chain on the `parent`, or root, container? – unwind Jan 29 '16 at 09:33
  • I just tried that, it turns out that in order to set the chain I should have first set the focus chain of the `self` to a list just containing the `container` (controls in this case), followed by a focus chain for self with the buttons in order. So I should have stacked them to point gtk to the right direction. @unwind could you make an answer out of this so I can mark it as the correct answer? `self.set_focus_chain([self.controls]) self.controls.set_focus_chain((self.controls.play, self.controls.backward, self.controls.recording, self.controls.end_inspection))` – B8vrede Jan 30 '16 at 11:02

1 Answers1

1

In order to set the focus chain for elements within containers it is required to first set the focus chain of the parent to the container. From there the focus chain for the container needs to be set.

Thus the code needs to be as follows:

# Points Gtk toward the container first
self.set_focus_chain([self.controls]) 

# Sets the focus chain inside the container
self.controls.set_focus_chain((self.controls.play, self.controls.backward, self.controls.recording, self.controls.end_inspection))

So it seems that GTK only allows focus chains towards children thus requiring users to set a chain of focus chain to set the focus chain to grand children.

B8vrede
  • 4,432
  • 3
  • 27
  • 40
  • 1
    Unfortunately, this appears to be deprecated since 3.24 (https://valadoc.org/gtk+-3.0/Gtk.Container.set_focus_chain.html) ☹ It says "For overriding focus behavior, use the GtkWidgetClass::focus signal." but doesn't explain how to recreate the focus chain behaviour. – IBBoard May 23 '20 at 15:43
  • 2
    Yeah, that is indeed super unclear, but there is an issue for it in the GTK issue tracker: https://gitlab.gnome.org/GNOME/gtk/-/issues/1430 – B8vrede May 25 '20 at 10:07
  • Interesting. That bug is tagged "GTK4". Does that mean that it's deprecated in 3.24 but not actually going to be removed in 3.x? It does appear that very little has happened so far beyond suggesting that the OP submit a pull request despite saying that there are multiple areas they don't understand and it's not clear how to use the referenced "focus" signals. – IBBoard May 27 '20 at 18:32