首页 > 解决方案 > 绘制鼠标移动 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 或线程的内部工作原理不够熟悉,无法确定原因。

标签: pythonmatplotlibpynput

解决方案


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

推荐阅读