如何检测 matplotlib 中的双击事件?
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
当我有 matplotlib 版本 1.1rc 时,我无法捕获 dblclick 事件。
后来我写了matplotlib 1.2的代码就可以了
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
我能够通过使用
from gtk.gdk import BUTTON_PRESS, _2BUTTON_PRESS 检测到这种情况。 _3BUTTON_PRESS
注意:在双击和三次点击枚举类型上使用下划线的原因并不是它们受到_protected,而是为了避免出现不允许的问题具有以数字开头的属性。您可以使用以下命令检查事件类型:
event.guiEvent.type
但是,我后来发现,如果您使用不同的后端,导入将导致异常(此外,我只遇到这个问题) GTKAgg' 后端)。所以现在我使用这样的结构:
如果有人有更干净的解决方案,请随意将其添加到此处。
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:
If anyone has a cleaner solution, feel free to add it here.
我在使用 matplotlib 1.1 时遇到了同样的问题。
没有“dblclick”事件类型。于是我就自己实现了。我要求两次点击之间的时间间隔必须小于0.5秒,否则程序将不执行任何操作。用户可以根据自己的经验自行调整此设置。
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.