How to prevent a double execution of a single-click event when actually trying to trigger a double click event?

StackOverflow https://stackoverflow.com/questions/22853158

  •  27-06-2023
  •  | 
  •  

Question

I am creating a gtk (pygtk) based GUI file browser on python using IconView. It has two signals - selection-changed which is triggered when an image is selected by single click and item-activated which is triggered when a double-click is performed . I need both the signals in my program , selection-changed to select folders to cut/copy and item-activated to navigate child folders and files.

However, double-clicking generates two selection-changed signals and one item-activated signal. I only need the item-activated signal to be generated.

Hence the question.

Was it helpful?

Solution

The second single-click and the double-click event should arrive simultaniously.

You can use glib.timeout_add(interval, callback, ...) to execute all the single-clicks after a milli second (or increase it). If within some time a double-click occurred then you do not execute the single-click event.

Implementation hints: use a variable so save when the last double-click occurred and a variable for the last click using time.time(). If the double-click and single-click are very close, do not execute the singe-click code. Due to the use of the timeout, the single-click always executes after the double-click.

OTHER TIPS

Why not keep track which item is selected? Ignore selection-changed on second click since selection won't have changed. Here is an example from pytk FAQs modified to handle this case...

import gtk
import gtk.gdk


current_path = []
current_path.append(None)

def on_selection_changed(iconview, current_path):
    if iconview.get_selected_items():
        if cmp(current_path[0], iconview.get_selected_items()[0]):
            print "selection-changed"
        current_path[0] = iconview.get_selected_items()[0]

def on_item_activated(iconview, path):
    print "item-activated"

# First create an iconview
view = gtk.IconView()
view.connect("selection-changed", on_selection_changed, current_path)
view.connect("item-activated", on_item_activated)

# Create a store for our iconview and fill it with stock icons
store = gtk.ListStore(str, gtk.gdk.Pixbuf)
for attr in dir(gtk):
    if attr.startswith('STOCK_'):
        stock_id = getattr(gtk, attr)
        pixbuf = view.render_icon(stock_id,
            size=gtk.ICON_SIZE_BUTTON, detail=None)
        if pixbuf:
            store.append(['gtk.%s' % attr, pixbuf])

# Connect our iconview with our store
view.set_model(store)
# Map store text and pixbuf columns to iconview
view.set_text_column(0)
view.set_pixbuf_column(1)

# Pack our iconview into a scrolled window
swin = gtk.ScrolledWindow()
swin.set_policy(gtk.POLICY_AUTOMATIC, gtk.POLICY_AUTOMATIC)
swin.add_with_viewport(view)
swin.show_all()

# pack the scrolled window into a simple dialog and run it
dialog = gtk.Dialog('IconView Demo')
close = dialog.add_button(gtk.STOCK_CLOSE, gtk.RESPONSE_NONE)
dialog.set_default_size(400,400)
dialog.vbox.pack_start(swin)
dialog.run()
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top