wxpython中如何区分鼠标的双击和单击 [英] How to distinguish the double click and single click of mouse in wxpython

查看:46
本文介绍了wxpython中如何区分鼠标的双击和单击的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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?

推荐答案

要区分单击和双击可以使用 wx.定时器:

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

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

Google 网上论坛中的类似讨论.

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

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

这篇关于wxpython中如何区分鼠标的双击和单击的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆