如何检测 matplotlib 中的双击事件?

发布于 2024-11-19 22:44:31 字数 443 浏览 2 评论 0原文

import matplotlib.pyplot as plt

def onclick(event):
    print(event.button)

fig = plt.figure()
connection_id = fig.canvas.mpl_connect('button_press_event', onclick)
plt.show()

对于此代码,问题是双击会命中 onclick() 处理程序三次次。我猜它正在接收点击事件以及额外的双击事件。

如何更改此行为,以便双击事件不会触发事件处理程序?或者,如何检测事件实例中的双击,以便忽略这种情况?

注意:button_release_event没有这个问题,但我想在button_press_event上触发

import matplotlib.pyplot as plt

def onclick(event):
    print(event.button)

fig = plt.figure()
connection_id = fig.canvas.mpl_connect('button_press_event', onclick)
plt.show()

With this code, the problem is double-clicks are hitting onclick() handler three times. I guess that it is receiving both the clicks, as well as an additional double-click event.

How can I change this behavior so that the event handler is not fired for double-click events? Alternatively, how can I detect a double-click from the event instance so that I can ignore that case?

Note: button_release_event does not have this problem, but I want to fire on the button_press_event

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

滥情稳全场 2024-11-26 22:44:31

当我有 matplotlib 版本 1.1rc 时,我无法捕获 dblclick 事件。
后来我写了matplotlib 1.2的代码就可以了

import matplotlib.pyplot as plt
fig = plt.figure()
def onclick(event):
    if event.dblclick:
         print event.button

connection_id = fig.canvas.mpl_connect('button_press_event', onclick)
plt.show()

When i had matplotlib version 1.1rc, i was not able to catch dblclick event.
Later, I wrote code for matplotlib 1.2 and that is ok

import matplotlib.pyplot as plt
fig = plt.figure()
def onclick(event):
    if event.dblclick:
         print event.button

connection_id = fig.canvas.mpl_connect('button_press_event', onclick)
plt.show()
噩梦成真你也成魔 2024-11-26 22:44:31

我能够通过使用

from gtk.gdk import BUTTON_PRESS, _2BUTTON_PRESS 检测到这种情况。 _3BUTTON_PRESS

注意:在双击和三次点击枚举类型上使用下划线的原因并不是它们受到_protected,而是为了避免出现不允许的问题具有以数字开头的属性。您可以使用以下命令检查事件类型:

event.guiEvent.type

但是,我后来发现,如果您使用不同的后端,导入将导致异常(此外,我只遇到这个问题) GTKAgg' 后端)。所以现在我使用这样的结构:

from gtk.gdk import BUTTON_PRESS as singleclick

if plt.get_backend() == 'GTKAgg':
    if hasattr(event, 'guiEvent') and event.guiEvent.type != singleclick:
        # suppress double click event
        return

如果有人有更干净的解决方案,请随意将其添加到此处。

I was able to detect the case by using

from gtk.gdk import BUTTON_PRESS, _2BUTTON_PRESS. _3BUTTON_PRESS

note: the reason for underscores on double and triple click enum types is not that they are _protected, but to dodge the issue where you aren't allowed to have an attribute starting with a number. You can check the event type with:

event.guiEvent.type

However, I later found out that the import will cause an exception if you are using a different backend (moreover, I only have this problem with 'GTKAgg' backend). So now I use a construct like this:

from gtk.gdk import BUTTON_PRESS as singleclick

if plt.get_backend() == 'GTKAgg':
    if hasattr(event, 'guiEvent') and event.guiEvent.type != singleclick:
        # suppress double click event
        return

If anyone has a cleaner solution, feel free to add it here.

来日方长 2024-11-26 22:44:31

我在使用 matplotlib 1.1 时遇到了同样的问题。
没有“dblclick”事件类型。于是我就自己实现了。我要求两次点击之间的时间间隔必须小于0.5秒,否则程序将不执行任何操作。用户可以根据自己的经验自行调整此设置。

import matplotlib.pyplot as plt
import time

fig = plt.figure()
one_click_trigger = False
time_first_click  = 0

def mouseDoubleClick(event):
    global one_click_trigger
    global time_first_click

    if one_click_trigger == False:
        one_click_trigger = True
        time_first_click = time.time()
        return
    else:
        double_click_interval = time.time() - time_first_click
        if double_click_interval > 0.5:
            one_click_trigger = False
            time_first_click = 0
            return

    print "Double click!"

fig.canvas.mpl_connect('button_press_event', mouseDoubleClick)
plt.show()

I had encountered the same problem when I was using matplotlib 1.1.
There was no 'dblclick' event type. So I implemented it by myself. I required the time interval between two clicks must be smaller than 0.5 sec or the program would do nothing. User could tune this setting by itself according to its experience.

import matplotlib.pyplot as plt
import time

fig = plt.figure()
one_click_trigger = False
time_first_click  = 0

def mouseDoubleClick(event):
    global one_click_trigger
    global time_first_click

    if one_click_trigger == False:
        one_click_trigger = True
        time_first_click = time.time()
        return
    else:
        double_click_interval = time.time() - time_first_click
        if double_click_interval > 0.5:
            one_click_trigger = False
            time_first_click = 0
            return

    print "Double click!"

fig.canvas.mpl_connect('button_press_event', mouseDoubleClick)
plt.show()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文