[pygtk] Focus Headache

Adriano Monteiro py.adriano at gmail.com
Mon Aug 7 08:30:44 WST 2006


Hi folks,

I've been trying all day long to make work a focus order, but I can't
find out how to succeed on it.

Here follows a cleanned code:


<code>
import gtk

class ScanCommandToolbar(gtk.HBox):
    def __init__(self):
        gtk.HBox.__init__(self)
        self.set_border_width(5)

        self.command_label = gtk.Label("Command:")
        self.command_entry = gtk.Entry()

        self.pack_start(self.command_label)
        self.pack_start(self.command_entry)

class ScanToolbar(gtk.HBox):
    def __init__(self):
        gtk.Box.__init__(self)

        self.scan_button = gtk.Button("Start Scan")
        self.target_label = gtk.Label("Target:")
        self.target_entry = gtk.ComboBoxEntry()
        self.profile_label = gtk.Label('Profile:')
        self.profile_entry = gtk.ComboBoxEntry()

        self.pack_start(self.target_label)
        self.pack_start(self.target_entry)
        self.pack_start(self.profile_label)
        self.pack_start(self.profile_entry)
        self.pack_start(self.scan_button)

        self.target_entry.child.grab_focus()

        self.target_entry.child.connect('key-press-event',\
                        self.next, self.profile_entry.child)

    def next(self, widget, event, next_widget):
        if event.hardware_keycode == 23:
            next_widget.grab_focus()

if __name__ == "__main__":
    w = gtk.Window()
    box = gtk.VBox()
    w.add(box)

    box.pack_start(ScanToolbar())
    box.pack_start(ScanCommandToolbar())

    w.connect("delete-event", lambda x,y: gtk.main_quit())
    w.show_all()
    gtk.main()
</code>

If you execute the code above, and try to navigate throught the
widgets using the tab key, you'll note that when the focus leaves the
target combo, to get into the profile combo, it doesn't focus the
profile combo's child. The focus goes to the combo button. So, if the
user would like to quickly type a target, followed by a tab and then
type a profile name, he will fail and will have to use the mouse to
click inside the combo to be able to write inside it. I tried to make
a hack to solve this problem, with the method next, but it doesn't
work.

I tried to define the toolbars focus_chain using the set_focus_chain
method, but it made everything worst them ever. When I set the focus
chain, the behavior is quite strange, and I just can't set focus on
any widget with the tab key. Maybe, I'm missing something... Can you
help me, guys?

Cheeeers!

-- 
Adriano Monteiro Marques
http://umit.sourceforge.net
py.adriano em gmail.com

"Don't stay in bed, unless you can make money in bed." - George Burns


More information about the pygtk mailing list