How to replace set_focus_chain() with "focus" signal?

Some time ago I wrote a desktop application using Python 2 and PyGTK 2. Recently I have been working on updating it to work with Python 3 and PyGObject and GTK3. In one part of this application I use set_focus_chain() to control which widgets automatically receive keyboard focus when pressing the TAB key or the Up or Down arrows.

Below I have pasted a short program that illustrates my use of set_focus_chain(). Note that the TAB key moves the keyboard focus from Entry to Entry, skipping both buttons in each frame. Also, pressing the Up and Down arrows moves the keyboard focus to the entry above or below (if possible).

This all works very well. However, the documentation for set_focus_chain() says that it is deprecated since version 3.24, and that one should use the “focus” signal instead. How would one accomplish the same thing using the “focus” signal instead?

Thank you very much for your help!
James

#!/usr/bin/python3

import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk

win = Gtk.Window()
win.set_border_width(5)
win.connect("destroy", Gtk.main_quit)

grid = Gtk.Grid(row_spacing=15, column_spacing=15)

for i in range(3):
    for j in range(3):
        frame = Gtk.Frame(label="Col: "+str(i)+"  Row: "+str(j))
        grid.attach(frame, i, j, 1, 1)
        frame.show()
        vbox = Gtk.Box(orientation=Gtk.Orientation.VERTICAL)
        vbox.set_border_width(5)
        frame.add(vbox)
        vbox.show()
        hbox = Gtk.Box()
        vbox.set_focus_chain([hbox])
        vbox.pack_start(hbox, False, False, 0)
        hbox.show()
        entry = Gtk.Entry()
        hbox.set_focus_chain([entry])
        entry.set_width_chars(10)
        hbox.pack_start(entry, False, False, 0)
        entry.show()
        button1 = Gtk.Button.new_with_label("Label1")
        hbox.pack_start(button1, False, False, 0)
        button1.show()
        button2 = Gtk.Button.new_with_label("Label2")
        vbox.pack_start(button2, False, False, 0)
        button2.show()

win.add(grid)
grid.show()
win.show()
Gtk.main()

I’d like to know the answer as well

https://gitlab.gnome.org/GNOME/gtk/issues/1430

Hi,

First, I think you should use following keys for focus:

  • GDK_KEY_Tab
  • GDK_KEY_ISO_Left_Tab

… or as you get button event of your pointer device.

But about above I am not sure. In my application arrow keys are used to navigate within an editor area, which already has the focus.

I think the most common way to set focus is using gtk_widget_grab_focus().

https://developer.gnome.org/gtk3/unstable/GtkWidget.html#gtk-widget-grab-focus
https://developer.gnome.org/pygtk/2.24/class-gtkwidget.html#method-gtkwidget--grab-focus

regards,
Joël

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.