Paste Description for PyGTK Tab-completion
A hacked-up version of the PyGTK EntryCompletion example which provides shell-like tab completion.
It also includes an adjustment to allow for specialized data sources to be hooked in (eg. SQL databases) if the expected GTK method is undesirable.
- PyGTK Tab-completion
- Monday, March 3rd, 2008 at 3:05:32am MST
- #!/usr/bin/env python
- import time
- import pygtk
- pygtk.require('2.0')
- import gtk
- KEY_TAB = 65289
- class CompleterEntry(gtk.Entry):
- def __init__(self, completion_getter):
- gtk.Entry.__init__(self)
- self.completion_getter = completion_getter
- self.completion = gtk.EntryCompletion()
- self.completion.set_model(None) # EntryCompletion is overzealous.
- # Only give it a model when we've explicitly asked for completion.
- self.completion.set_inline_selection(True)
- self.set_completion(self.completion)
- self.completion.set_minimum_key_length(1)
- self.completion.set_text_column(0)
- self.connect('key-press-event', self.entry_keypress_cb)
- self.completion.connect('match-selected', self.match_cb)
- def entry_keypress_cb(self, widget, event):
- if event.keyval == KEY_TAB:
- liststore = self.completion_getter(self.get_text())
- if len(liststore) == 1:
- self.set_text(liststore[0][0])
- self.set_position(-1)
- else:
- self.completion.set_model(liststore)
- self.completion.complete()
- gtk.main_do_event(gtk.gdk.Event(gtk.gdk.KEY_PRESS))
- return True
- else:
- self.completion.set_model(None)
- return False
- def match_cb(self, completion, model, iter):
- print model[iter][0], 'was selected'
- completion.set_model(None)
- return
- def get_completion(prefix):
- print "TODO: Build completion list from DB"
- liststore = gtk.ListStore(str)
- for s in ['apple', 'banana', 'cap', 'comb', 'color',
- 'dog', 'doghouse']:
- if s.startswith(prefix):
- liststore.append([s])
- return liststore
- class EntryCompletionExample:
- def __init__(self):
- window = gtk.Window()
- window.connect('destroy', lambda w: gtk.main_quit())
- vbox = gtk.VBox()
- label = gtk.Label('Type a, b, c or d\nfor completion')
- vbox.pack_start(label)
- entry = CompleterEntry(get_completion)
- vbox.pack_start(entry)
- window.add(vbox)
- window.show_all()
- entry.connect('activate', self.activate_cb)
- return
- def activate_cb(self, entry):
- if entry.get_text():
- print "TODO: Apply the tag"
- entry.set_text("")
- else:
- print "TODO: Go to the next image"
- return
- def main():
- gtk.main()
- return
- if __name__ == "__main__":
- ee = EntryCompletionExample()
- main()
Paste Details
Tags: python pygtk tab-completion completion
advertising
Update the Post
Either update this post and resubmit it with changes, or make a new post.
You may also comment on this post.
Please note that information posted here will expire by default in one month. If you do not want it to expire, please set the expiry time above. If it is set to expire, web search engines will not be allowed to index it prior to it expiring. Items that are not marked to expire will be indexable by search engines. Be careful with your passwords. All illegal activities will be reported and any information will be handed over to the authorities, so be good.