使用Zelle graphics.py在Python中获取鼠标跟踪

使用Zelle graphics.py在Python中获取鼠标跟踪,python,zelle-graphics,Python,Zelle Graphics,我是Python新手。我需要编写一个程序来移动我的球或圆圈时,我点击鼠标。我如何做到这一点?我有以下开始使用的代码: from graphics import * import time def MouseTracker(): win = GraphWin("MyWindow", 500, 500) win.setBackground("blue") cir = Circle(Point(250,250) ,20) cir.setFill("red") cir.draw(win) whil

我是Python新手。我需要编写一个程序来移动我的球或圆圈时,我点击鼠标。我如何做到这一点?我有以下开始使用的代码:

from graphics import *
import time

def MouseTracker():

win = GraphWin("MyWindow", 500, 500)
win.setBackground("blue")
cir = Circle(Point(250,250) ,20)
cir.setFill("red")
cir.draw(win)

while(win.getMouse() != None):
    xincr = 0
    yincr = 0
for i in range(7):
    cir.move(xincr, yincr)
    time.sleep(.2)
win.getMouse()

假设您没有绑定到某些特定的工具或实现,您可能会发现matplotlib很有用。可以使用圆面片()将圆打印到绘图区域上,然后在图形轴中单击鼠标时将其四处移动。您需要连接到事件单击侦听器,并定义一个处理图形更新的回调函数-有关如何执行此操作的示例,请参见。您可以使用扩展数据和ydata方法获得鼠标按下的坐标

这在python 2.7中适用:

import matplotlib.pyplot as plt
from matplotlib.patches import Circle

fig = plt.figure()
ax = fig.add_subplot(111)
circ = Circle((0.5,0.5), 0.1)
ax.add_patch(circ)

def update_circle(event):
    ax.cla()
    circ = Circle((event.xdata, event.ydata), 0.1)
    ax.add_patch(circ)
    fig.canvas.draw()

fig.canvas.mpl_connect('button_press_event', update_circle)
plt.show()

假设您没有绑定到某些特定的工具或实现,您可能会发现matplotlib很有用。可以使用圆面片()将圆打印到绘图区域上,然后在图形轴中单击鼠标时将其四处移动。您需要连接到事件单击侦听器,并定义一个处理图形更新的回调函数-有关如何执行此操作的示例,请参见。您可以使用扩展数据和ydata方法获得鼠标按下的坐标

这在python 2.7中适用:

import matplotlib.pyplot as plt
from matplotlib.patches import Circle

fig = plt.figure()
ax = fig.add_subplot(111)
circ = Circle((0.5,0.5), 0.1)
ax.add_patch(circ)

def update_circle(event):
    ax.cla()
    circ = Circle((event.xdata, event.ydata), 0.1)
    ax.add_patch(circ)
    fig.canvas.draw()

fig.canvas.mpl_connect('button_press_event', update_circle)
plt.show()

假设您想继续使用开始使用的图形包,您可以这样做,但缺少保存鼠标位置并将其与圆的中心位置进行比较的代码:

from graphics import *

WIDTH, HEIGHT = 500, 500
POSITION = Point(250, 250)
RADIUS = 20
STEPS = 7

def MouseTracker(window, shape):
    while True:
        position = window.getMouse()

        if position != None:  # in case we want to use checkMouse() later
            center = shape.getCenter()
            xincr = (position.getX() - center.getX()) / STEPS
            yincr = (position.getY() - center.getY()) / STEPS
            for _ in range(STEPS):
                shape.move(xincr, yincr)

win = GraphWin("MyWindow", WIDTH, HEIGHT)
win.setBackground("blue")

cir = Circle(POSITION, RADIUS)
cir.setFill("red")
cir.draw(win)

MouseTracker(win, cir)

您需要关闭窗口以打破跟踪循环——在实际的程序中,您需要将此作为设计的一部分来处理(即,某些用户操作会导致
中中断
,而True:
循环)。

假设您希望继续使用开始使用的图形包,可以这样做,但缺少保存鼠标位置并将其与圆的中心位置进行比较的代码:

from graphics import *

WIDTH, HEIGHT = 500, 500
POSITION = Point(250, 250)
RADIUS = 20
STEPS = 7

def MouseTracker(window, shape):
    while True:
        position = window.getMouse()

        if position != None:  # in case we want to use checkMouse() later
            center = shape.getCenter()
            xincr = (position.getX() - center.getX()) / STEPS
            yincr = (position.getY() - center.getY()) / STEPS
            for _ in range(STEPS):
                shape.move(xincr, yincr)

win = GraphWin("MyWindow", WIDTH, HEIGHT)
win.setBackground("blue")

cir = Circle(POSITION, RADIUS)
cir.setFill("red")
cir.draw(win)

MouseTracker(win, cir)
您需要关闭窗口以打破跟踪循环——在实际程序中,您需要将此作为设计的一部分来处理(即,某些用户操作会导致
中出现
中断
,而True:
循环)