gpt4 book ai didi

python - Matplotlib B1-Motion(按住键的鼠标运动)等效?

转载 作者:太空宇宙 更新时间:2023-11-04 10:22:50 25 4
gpt4 key购买 nike

是否有类似于 Tkinter 的 matplotlib 事件 <B1-Motion> key ?这样只有当用户在移动鼠标时按住 B1 时才会调用函数?

我目前正在调查 this文档,但没有看到这样的选项,如果没有,是否有一些简单的方法可以重新创建它?

def notify_motion(event):
"""
Only called when button 1 is clicked and motion detected
"""

...

最佳答案

我不知道你是否仍然关心这个问题,无论如何有一个非常简单的解决方案,使用matplotlib.backend_bases.MouseEventbutton 属性。类,因为如果光标被按下,event.button 将为 1,如果光标被释放,则为 None

以下是我通过按住并移动光标来画线的代码:

import matplotlib.pyplot as plt
import numpy as np

def moved_and_pressed(event):
if event.button==1:
x = np.append(line.get_xdata(), event.xdata)
y = np.append(line.get_ydata(), event.ydata)
line.set_data(x, y)
fig.canvas.draw()

fig, ax = plt.subplots(1,1, figsize=(5,3), dpi=100)
line, = ax.plot([], [], 'k')
ax.set_xlim(0,10); ax.set_ylim(0,10)
cid = fig.canvas.mpl_connect('motion_notify_event', moved_and_pressed)
plt.show()

希望对您有所帮助。

关于python - Matplotlib B1-Motion(按住键的鼠标运动)等效?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31248228/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com