1

I'm trying to setup a window with a notebook and some pages. Within the pages, there will be some entries (see example code).

I'd like to handle the tab key and arrow keys on my own. I don't want the arrow up key to jump to the page title, I don't want the arrow left / right key to cycle through the pages. I don't want the tab key to cycle through the entries.

Connecting to the key-press signal and checking for tab or arrow keys does not really work.

I tried to change the focus chain, but it still focusses on the page title or cycles through the pages with left / rigth arrow.

Any help / idea is highly appreciated.

#!/usr/bin/env python

import pygtk
import gtk

class NotebookExample:


    def __init__(self):
        window = gtk.Window(gtk.WINDOW_TOPLEVEL)
        window.set_size_request(200,200)

        notebook = gtk.Notebook()
        notebook.set_tab_pos(gtk.POS_TOP)

        notebook.show()

        hbox1 = gtk.HBox()
        notebook.append_page(hbox1, gtk.Label("Page1"))
        hbox2 = gtk.HBox()
        notebook.append_page(hbox2, gtk.Label("Page2"))

        window.add(notebook)

        entry1 = gtk.Entry()
        entry2 = gtk.Entry()
        entry3 = gtk.Entry()
        entry1.set_width_chars(5)
        entry2.set_width_chars(5)
        entry3.set_width_chars(5)

        hbox1.pack_start(entry1, False, False, 10)
        hbox1.pack_start(entry2, False, False, 10)
        hbox1.pack_start(entry3, False, False, 10)

        hbox1.set_focus_chain([entry2, entry3])

        window.show_all()

def main():
    gtk.main()
    return 0

if __name__ == "__main__":
    NotebookExample()
    main()
Murat G
  • 61
  • 1
  • 7

1 Answers1

1

Found it.

Listening to the key-press-event and returning "True to stop other handlers from being invoked for the event. False to propagate the event further.

        window.connect("key-press-event", self.do_key_press)

    def do_key_press(self, widget, event):
        if gtk.gdk.keyval_name(event.keyval) == 'Tab':
            print "tab"
            return True
        if gtk.gdk.keyval_name(event.keyval) == 'Left' or gtk.gdk.keyval_name(event.keyval) == 'Right':
            print "cursor"
            return True
        return
Murat G
  • 61
  • 1
  • 7