Osmo Maatta wrote:
Hello,

I have a small application that has a search-field (gtk.ComboBoxEntry) and a button to start a search job.

The application will start a new search if user:
1) Selects an item from the gtk.ComboBoxEntry's drop-down list.
    This is handled by gtk.ComboBoxEntry's "changed" event. This is Ok.

2  Types a search query (some text) and presses the [Start search] button.
    This is handled by the button's "clicked" event. This is Ok.

3) Presses the [Enter]-key.
This is handled by the combo's "activate" event attached to the gtk.Entry. This is Ok.

Because a search-job can take quite a long time, I do not want to activate search for each keystroke, when user types text in the search field. And this is my problem. Key-strokes activate the "changed" event on the gtk.ComboBoxEntry field. I cannot distinguish between the drop-down list's "changed" event (which I want) and the "changed" event when user types text (I try to avoid this event).

User is welcome to type in some text/query and press the [Start search] button. This is OK.

So how to avoid "changed" event on each keystroke?

A simplified but complete example: http://www.futuredesktop.com/tmp/w4.py

Happy Easter,
  Moma Antero
  Grønland




I couldn't find a direct way of handling the problem..

But how about a timeout that only occurs after the user stops typing for a while (e.g. 1/2 a second). It stops the search being triggered for every keystroke.




# -*- coding: utf-8 -*-
import sys
import gtk
import gobject

class TestWindow(gtk.Window):
    __gtype_name__ = "TestWindow"

    def __init__(self, *args):
        gtk.Window.__init__(self, *args)
        self.timeout = None

        self.set_default_size(300, 200)

        vbox = gtk.VBox()
        self.add(vbox)

        hbox = gtk.HBox()
        vbox.pack_start(hbox, expand=False, fill=False, padding=0)

        combo = gtk.combo_box_entry_new_text()
        #combo.connect("key-press-event", self.combo_key_press_event)
        #combo.connect("key-release-event", self.combo_key_press_event)
        #combo.connect("changed", self.combo_selection_changed)
        combo.connect("changed", self.combo_timeout)
        hbox.pack_start(combo, expand=False, fill=False, padding=0)

        # Add some items
        for i in range(6):
            combo.append_text("Item %d" % (i,))

        # Text input field (gtk.Entry object)
        entry = combo.get_children()[0]
        # Activate called on Enter-key
        entry.connect("activate", self.entry_activate_event, combo)

        button = gtk.Button("Start search")
        button.connect("clicked", self.start_search_cb, combo)
        hbox.pack_start(button, expand=False, fill=False, padding=0)

        self.show_all()

    def entry_activate_event(self, widget, user_data):
        combo = user_data
        txt = combo.get_active_text()
        print "You pressed enter:", txt
        return True

    def combo_timeout(self, widget):
        if self.timeout :
            gobject.source_remove(self.timeout)		# remove old timeout
        self.timeout = gobject.timeout_add(500, self.combo_selection_changed, widget)	# add a new one

    def combo_selection_changed(self, widget):
        txt =  widget.get_active_text()
        print "Selection changed:", txt
        return False	# cancal the timeout

    def start_search_cb(self, widget, user_data):
        combo = user_data
        txt = combo.get_active_text()
        print "Start search for:", txt 


if __name__ == "__main__":
    window = TestWindow()
    window.show()
    gtk.main()

_______________________________________________
pygtk mailing list   pygtk@daa.com.au
http://www.daa.com.au/mailman/listinfo/pygtk
Read the PyGTK FAQ: http://faq.pygtk.org/

Reply via email to