Part of Slepp's ProjectsPastebinTURLImagebinFilebin
Feedback -- English French German Japanese
Create Upload Newest Tools Donate
Sign In | Create Account

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 

  1. #!/usr/bin/env python
  2.  
  3. import time
  4. import pygtk
  5. pygtk.require('2.0')
  6. import gtk
  7.  
  8. KEY_TAB = 65289
  9.  
  10. class CompleterEntry(gtk.Entry):
  11.         def __init__(self, completion_getter):
  12.                 gtk.Entry.__init__(self)
  13.  
  14.                 self.completion_getter = completion_getter
  15.  
  16.                 self.completion = gtk.EntryCompletion()
  17.                 self.completion.set_model(None) # EntryCompletion is overzealous.
  18.                 # Only give it a model when we've explicitly asked for completion.
  19.                 self.completion.set_inline_selection(True)
  20.                 self.set_completion(self.completion)
  21.                 self.completion.set_minimum_key_length(1)
  22.                 self.completion.set_text_column(0)
  23.  
  24.                 self.connect('key-press-event', self.entry_keypress_cb)
  25.                 self.completion.connect('match-selected', self.match_cb)
  26.  
  27.         def entry_keypress_cb(self, widget, event):
  28.                 if event.keyval == KEY_TAB:
  29.                         liststore = self.completion_getter(self.get_text())
  30.  
  31.                         if len(liststore) == 1:
  32.                                 self.set_text(liststore[0][0])
  33.                                 self.set_position(-1)
  34.                         else:
  35.                                 self.completion.set_model(liststore)
  36.                                 self.completion.complete()
  37.                                 gtk.main_do_event(gtk.gdk.Event(gtk.gdk.KEY_PRESS))
  38.                         return True
  39.                 else:
  40.                         self.completion.set_model(None)
  41.                         return False
  42.  
  43.         def match_cb(self, completion, model, iter):
  44.                 print model[iter][0], 'was selected'
  45.                 completion.set_model(None)
  46.                 return
  47.  
  48. def get_completion(prefix):
  49.         print "TODO: Build completion list from DB"
  50.         liststore = gtk.ListStore(str)
  51.         for s in ['apple', 'banana', 'cap', 'comb', 'color',
  52.                         'dog', 'doghouse']:
  53.                 if s.startswith(prefix):
  54.                         liststore.append([s])
  55.         return liststore
  56.  
  57. class EntryCompletionExample:
  58.         def __init__(self):
  59.                 window = gtk.Window()
  60.                 window.connect('destroy', lambda w: gtk.main_quit())
  61.                 vbox = gtk.VBox()
  62.                 label = gtk.Label('Type a, b, c or d\nfor completion')
  63.                 vbox.pack_start(label)
  64.  
  65.                 entry = CompleterEntry(get_completion)
  66.  
  67.                 vbox.pack_start(entry)
  68.                 window.add(vbox)
  69.                 window.show_all()
  70.                 entry.connect('activate', self.activate_cb)
  71.                 return
  72.  
  73.         def activate_cb(self, entry):
  74.                 if entry.get_text():
  75.                         print "TODO: Apply the tag"
  76.                         entry.set_text("")
  77.                 else:
  78.                         print "TODO: Go to the next image"
  79.                 return
  80.  
  81. def main():
  82.         gtk.main()
  83.         return
  84.  
  85. if __name__ == "__main__":
  86.         ee = EntryCompletionExample()
  87.         main()

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.

update paste below
details of the post (optional)

Note: Only the paste content is required, though the following information can be useful to others.

Save name / title?

(space separated, optional)



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.

worth-right