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

后端 未结 1 1423
孤街浪徒
孤街浪徒 2021-01-25 09:21

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

相关标签:
1条回答
  • 2021-01-25 09:36

    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()
    
    0 讨论(0)
提交回复
热议问题