home *** CD-ROM | disk | FTP | other *** search
/ tusportal.tus.k12.pa.us / tusportal.tus.k12.pa.us.tar / tusportal.tus.k12.pa.us / Wyse / latest-image.raw / 0.img / usr / lib / pygtk / 2.0 / demos / entry_completion.py < prev    next >
Text File  |  2010-05-11  |  2KB  |  77 lines

  1. #!/usr/bin/env python
  2. '''Entry Completion
  3.  
  4. GtkEntryCompletion provides a mechanism for adding support for
  5. completion in GtkEntry.
  6. '''
  7. # pygtk version: Maik Hertha <maik.hertha@berlin.de>
  8.  
  9. import gtk
  10.  
  11. class EntryCompletionDemo(gtk.Dialog):
  12.  
  13.     def __init__(self, parent=None):
  14.         gtk.Dialog.__init__(self, self.__class__.__name__, parent,
  15.             0,
  16.             (gtk.STOCK_CLOSE, gtk.RESPONSE_NONE))
  17.         try:
  18.             self.set_screen(parent.get_screen())
  19.         except AttributeError:
  20.             self.connect('destroy', lambda *w: gtk.main_quit())
  21.         self.connect("response", lambda d, r: d.destroy())
  22.         self.set_resizable(False)
  23.  
  24.         vbox = gtk.VBox(False, 5)
  25.         self.vbox.pack_start(vbox, True, True, 0)
  26.         vbox.set_border_width(5)
  27.  
  28.         label = gtk.Label()
  29.         label.set_markup("Completion demo, try writing <b>total</b> "
  30.             "or <b>gnome</b> for example.")
  31.         vbox.pack_start(label, False, False, 0)
  32.  
  33.         # Create our entry
  34.         entry = gtk.Entry()
  35.         vbox.pack_start(entry, False, False, 0)
  36.  
  37.         # Create the completion object
  38.         completion = gtk.EntryCompletion()
  39.  
  40.         # Assign the completion to the entry
  41.         entry.set_completion(completion)
  42.  
  43.         # Create a tree model and use it as the completion model
  44.         completion_model = self.__create_completion_model()
  45.         completion.set_model(completion_model)
  46.  
  47.         # Use model column 0 as the text column
  48.         completion.set_text_column(0)
  49.  
  50.         self.show_all()
  51.  
  52.     def __create_completion_model(self):
  53.         ''' Creates a tree model containing the completions.
  54.         '''
  55.         store = gtk.ListStore(str)
  56.  
  57.         # Append one word
  58.         iter = store.append()
  59.         store.set(iter, 0, "GNOME")
  60.  
  61.         # Append another word
  62.         iter = store.append()
  63.         store.set(iter, 0, "total")
  64.  
  65.         # And another word
  66.         iter = store.append()
  67.         store.set(iter, 0, "totally")
  68.  
  69.         return store
  70.  
  71. def main():
  72.     EntryCompletionDemo()
  73.     gtk.main()
  74.  
  75. if __name__ == '__main__':
  76.     main()
  77.