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!


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

win = Gtk.Window()
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)
        vbox = Gtk.Box(orientation=Gtk.Orientation.VERTICAL)
        hbox = Gtk.Box()
        vbox.pack_start(hbox, False, False, 0)
        entry = Gtk.Entry()
        hbox.pack_start(entry, False, False, 0)
        button1 = Gtk.Button.new_with_label("Label1")
        hbox.pack_start(button1, False, False, 0)
        button2 = Gtk.Button.new_with_label("Label2")
        vbox.pack_start(button2, False, False, 0)


I’d like to know the answer as well


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


1 Like

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