Re: Combined search and combobox?



On 07/26/2018 07:36 AM, Dov Grobgeld via gtk-devel-list wrote:
Is there a widget that combines a searchbox with a combobox?

A use case would be to search for a fontname in a very long font list.

I would like to be able to type a search string, and have the opened combobox display only entries that match the typed string. A plus would be if it is possible to change how matches take place, e.g. between multiple word (like helm-mode in emacs), a regular _expression_, or an absolute match.

Has someone written anything like that?

You didn't specify a language, so here's an example in Python. It uses space separated keywords, like in Google search or so. It may not be exactly as you requested, but will give you something to start with.

#! /usr/bin/env python3

import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk
import os, sys


class GUI (Gtk.Window):
    def __init__(self):

        Gtk.Window.__init__(self, title="Combo with search")
        self.model = Gtk.ListStore(str)
        self.populate_model()
        #combobox
        combo = Gtk.ComboBox.new_with_model_and_entry(model = self.model)
        combo.set_entry_text_column(0)
        combo.connect('changed', self.changed)
        #completion
        completion = Gtk.EntryCompletion ()
        completion.set_model(self.model)
        completion.set_text_column(0)
        completion.set_match_func(self.match_func)
        completion.connect ('match-selected', self.match_selected)
        #combobox entry
        entry = combo.get_child()
        entry.set_completion (completion)
        #main window
        self.add (combo)
        self.show_all()

    def changed (self, combo):
        _iter = combo.get_active_iter()
        if _iter != None:
            font = self.model[_iter][0]
            print ('You selected combo:', font)

    def match_selected (self, completion, model, _iter):
        print ('You selected completion:', model[_iter][0])

    def match_func (self, completion, string, _iter):
        for word in string.split():
            if word not in self.model[_iter][0].lower(): #search is always lower case
                return False
        return True

    def on_window_destroy(self, window):
        Gtk.main_quit()

    def populate_model (self):
        for i in range (100):
            self.model.append(["Font %d" % i])

def main():
    app = GUI()
    Gtk.main()
       
if __name__ == "__main__":
    sys.exit(main())


Reuben




[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]