python - 绘制鼠标移动 Python
问题描述
我想使用 matplotlib 和 pynput 近乎实时地绘制鼠标的移动,但我怀疑我遇到了一些代码被阻止的问题。代码使用此答案的简化版本。
import matplotlib.pyplot as plt
from pynput import mouse
from time import sleep
fig, ax = plt.subplots()
ax.set_xlim(0, 1920-1)
ax.set_ylim(0, 1080-1)
plt.show(False)
plt.draw()
x,y = [0,0]
points = ax.plot(x, y, 'o')[0]
# cache the background
background = fig.canvas.copy_from_bbox(ax.bbox)
def on_move(x, y):
points.set_data(x,y)
# restore background
fig.canvas.restore_region(background)
# redraw just the points
ax.draw_artist(points)
# fill in the axes rectangle
fig.canvas.blit(ax.bbox)
with mouse.Listener(on_move=on_move) as listener:
sleep(10)
代码似乎停止在ax.draw_artist(points)
. pynput 鼠标侦听器是 a threading.Thread
,所有回调都是从线程调用的。我对 matplotlib 或线程的内部工作原理不够熟悉,无法确定原因。
解决方案
与 matplotlib GUI 并行运行具有 GUI 输入的线程可能会导致问题。
无论如何,只使用 matplotlib 工具可能更有意义。有一个可用的事件处理机制,它提供了一个"motion_notify_event"
用于获取当前鼠标位置的方法。为该事件注册的回调将存储鼠标位置并 blit 更新的点。
import matplotlib.pyplot as plt
fig, ax = plt.subplots()
ax.set_xlim(0, 1920-1)
ax.set_ylim(0, 1080-1)
x,y = [0], [0]
# create empty plot
points, = ax.plot([], [], 'o')
# cache the background
background = fig.canvas.copy_from_bbox(ax.bbox)
def on_move(event):
# append event's data to lists
x.append(event.xdata)
y.append(event.ydata)
# update plot's data
points.set_data(x,y)
# restore background
fig.canvas.restore_region(background)
# redraw just the points
ax.draw_artist(points)
# fill in the axes rectangle
fig.canvas.blit(ax.bbox)
fig.canvas.mpl_connect("motion_notify_event", on_move)
plt.show()
推荐阅读
- tcl - tcl 浮点数处理
- javascript - 使用动态键和对象值反应 setState 未按预期工作
- regex - 将变量中的字符串与另一个字符串中的字符串完全匹配
- doctrine-orm - 使用 DQL 仅保留最新记录
- python-3.x - 如何在 python3 中创建一个包含 os.system 的函数?
- php - JSON对象数组逗号后没有空格
- vbscript - VBScript 仅将空 (265K) PST 从网络文件夹复制到网络文件夹
- c# - 由于 ModalPopupExtender 和 ASP:Panel,LinkButton On_Command 事件无法正常工作
- keras - 用于分析长一维序列的卷积自动编码器
- neo4j - Neo4J Desktop 1.1.8 安装错误(社区版)