PYGTK redirect event to TreeView
In PyGTK, I have an Entry and a TreeView. When a TreeView is focused, the key events (Up, Down, PageUp, PageDown) move selection in the view in a certain way. I want to intercept these key events when the Entry is focu开发者_JAVA百科sed, and redirect them to the TreeView so that the selection is moved as though the TreeView was focused.
I can intercept the key press events on the Entry and determine if it's for the keys I need, but I have trouble with passing it to the TreeView.
# In UI initialization
self.name_entry = gtk.Entry(max=0)
self.name_entry.connect('key-press-event', self.on_key_press)
store = self.create_store() # a simple ListStore is created here
view = gtk.TreeView(store)
rendererText = gtk.CellRendererText()
column = gtk.TreeViewColumn("Name", rendererText, text=0)
column.set_sort_column_id(0)
view.append_column(column)
self.tree_view = view
# ...
def on_key_press(self, widget, event):
if event.keyval == UP:
self.tree_view.do_something() # ???
return True
# etc. for other keyvals
Is there a way to make tree_view handle the event, as though the key was pressed while it had focus?
(Note: the program is a hack; I don't care for the best practices of PyGTK development here.)
Any help is appreciated.
Something like this should work:
def on_key_press(self, widget, event):
if gtk.gdk.keyval_name(event.keyval) in ("Up", "Down", "Page_Up", "Page_Down"):
self.tree_view.grab_focus()
self.tree_view.emit('key_press_event', event)
self.name_entry.grab_focus()
精彩评论