我想使用matplotlib和pynput近乎实时地绘制鼠标的移动,但我怀疑我遇到了一些被阻止的代码问题.代码使用的是
this answer的简化版本.
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(点)上. pynput鼠标侦听器是一个threading.Thread,并且所有回调都是从线程调用的.我对matplotlib或线程的内部工作原理不太熟悉,无法确定原因.