Samuel
Samuel

Reputation: 6237

How to distinguish the double click and single click of mouse in wxpython

Q1: Is there a mouse single click event in wxpython. I didn't find a single click event. So I use Mouse_Down and Mouse_UP to implement that.
Q2: I also have a double click event. But double click event can emmit mouse up and down also. How can I distinguish them?

Upvotes: 2

Views: 1776

Answers (1)

Nigel Tufnel
Nigel Tufnel

Reputation: 11524

To distinguish click and double click you can use wx.Timer:

  1. Start the timer in OnMouseDown
  2. In OnDoubleClick event handler stop the timer
  3. If timer wasn't stopped by OnDoubleClick you can handle single click in the timer handler.

Similar discussion in Google Groups.

Code example (needs polishing and testing of course but gives a basic idea):

import wx

TIMER_ID = 100

class Frame(wx.Frame):
    def __init__(self, title):
        wx.Frame.__init__(self, None, title=title, size=(350,200))
        self.timer = None
        self.Bind(wx.EVT_LEFT_DCLICK, self.OnDoubleClick)
        self.Bind(wx.EVT_LEFT_DOWN, self.OnLeftDown)

    def OnDoubleClick(self, event):
        self.timer.Stop()
        print("double click")

    def OnSingleClick(self, event):
        print("single click")
        self.timer.Stop()

    def OnLeftDown(self, event):
        self.timer = wx.Timer(self, TIMER_ID)
        self.timer.Start(200) # 0.2 seconds delay
        wx.EVT_TIMER(self, TIMER_ID, self.OnSingleClick)



app = wx.App(redirect=True)
top = Frame("Hello World")
top.Show()
app.MainLoop()

Upvotes: 4

Related Questions