getMouse in python using graphics
Question: I am very new to Python. I need to write a program to move my ball or circle
when I click the mouse. How do I achieve this? I have the below code that I
got started with.

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


Answer: Assuming you are not bound to some specific tools or implementation, you may
find matplotlib useful. You can plot a circle onto the drawing area using a
circle patch (<http://matplotlib.org/api/patches_api.html>) and then move it
around when there is mouse-click in the graph axes. You will need to connect
to the event-click listener and define a callback function which handles the
drawing update - see <http://matplotlib.org/users/event_handling.html> for
examples of how to do this. You can get the coordinates of the mouse press
using the xdata and ydata methods.

This worked for me in 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()
    

