4

I wrote a code that handles different events for both - mouse single-click and double click. The problem is that each time that user double-click the mouse it triggers also single-click and after that trigger the double-click event.

I want that double-click will trigger only one event!! the double-click event.

Any suggestions?

Thanks

4
  • Could you maybe post the code that you have written (possibly as a Minimal, Complete, and Verifiable example)? Commented Nov 29, 2018 at 8:33
  • Hi Thomas, my code is bit long and I think it will not contribute.. the idea is that I have two different functions - one for single click and one for double click and I want that double-click will trigger only the double click function Commented Nov 29, 2018 at 8:43
  • Possible duplicate of Distinguish between single and double click with matplotlib Commented Nov 29, 2018 at 9:47
  • I saw it, no answer there... The idea is clear but how to implement it.. Commented Nov 29, 2018 at 9:51

2 Answers 2

6

As linked by @ThomasKühn, the answer is to create a software debounce. There are several ways to go about it, and the solution probably depends on your application (are you using a GUI, what backend, etc.) To be as agnostic as possible, I've implemented my solution using a one-shot thread from the threading module.

import threading
import matplotlib.pyplot as plt

DEBOUNCE_DUR = 0.25
t = None


def on_press(event):
    global t
    if t is None:
        t = threading.Timer(DEBOUNCE_DUR, on_singleclick, [event])
        t.start()
    if event.dblclick:
        t.cancel()
        on_dblclick(event)


def on_dblclick(event):
    global t
    print("You double-clicked", event.button, event.xdata, event.ydata)
    t = None


def on_singleclick(event):
    global t
    print("You single-clicked", event.button, event.xdata, event.ydata)
    t = None


fig, ax = plt.subplots()
cid = fig.canvas.mpl_connect('button_press_event', on_press)

plt.show()
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks! looks as the solution I have looked for!
since Matplotlib is not thread save, this doesnt work for me. It registers the doubleclick, but afterwards it only can register doubleclicks
1

As I was looking for a solution for a embedded Matplotlib canvas in Tkinter, I came up for another solution (since the accepted answer doesn't work quite well in that case) here is a codesnipped which will hopefully help others struggling with tkinter and matplotlib:

def _on_click_debounce(self,event):
    if self._job is None:
        self._job = root.after(self.DEBOUNCE_DUR, lambda: self._on_click(event))
    if event.dblclick:
        root.after_cancel(self._job)
        self._job = None
        self._on_dblclick(event)

def _on_dblclick(self,event):
    print('dblclick!')

def _on_click(self,event):
    print('singleclick!')
    self._job = None

The function _on_click_debounce gets handed over to the matplotlib eventhandling (fig.canvas.mpl_connect('button_press_event', _on_click_debounce)) and root is just the global root = tkinter.Tk() of tkinter

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.