简体   繁体   中英

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?

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()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM