【问题标题】:How to distinguish the double click and single click of mouse in wxpythonwxpython中如何区分鼠标的双击和单击
【发布时间】:2014-01-08 09:04:58
【问题描述】:

Q1:wxpython中是否有鼠标单击事件。我没有找到单击事件。所以我使用Mouse_DownMouse_UP 来实现它。
Q2:我还有一个双击事件。但是双击事件也可以上下鼠标。如何区分它们?

【问题讨论】:

    标签: python wxpython


    【解决方案1】:

    要区分点击和双击可以使用wx.Timer:

    1. 在 OnMouseDown 中启动计时器
    2. 在 OnDoubleClick 事件处理程序中停止计时器
    3. 如果 OnDoubleClick 没有停止计时器,您可以在计时器处理程序中处理单击。

    Similar discussion in Google Groups.

    代码示例(当然需要完善和测试,但给出一个基本概念):

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

    【讨论】:

    • 没有必要在每个左下重新绑定 wx.EVT_TIMER 事件处理程序。我会将其移至 init 方法并将其切换为使用 self.Bind 语法。我还会重复使用相同的计时器对象,而不是每次都创建一个新对象。
    猜你喜欢
    • 2010-12-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多