2

I used Zelle's "graphics.py" file. I use Thonny. I want to use function "getMouse() & getMouseNow()", but these messages come up. What should I do ? Help Me !

Code :

from graphics import *

def draw():
    win = GraphWin("My Circle", 500, 500)
    circle = Circle(Point(150,150), 50)
    circle.draw(win)
    p = win.getMouse()
    x = p.x
    y = p.y
    circle.moveTo(x,y)

draw()

Output :

Creates a window of above dimensions and a circle in it. After clicking into the window ...

Traceback (most recent call last):
    File "C:\Users\Shivam\Documents\Python\Mouse.py", line 13, in <module>
        draw()
    File "C:\Users\Shivam\Documents\Python\Mouse.py", line 10, in draw
        m = Circle.moveTo(x,y)
AttributeError: type object 'Circle' has no attribute 'moveTo'
cdlane
  • 40,441
  • 5
  • 32
  • 81

2 Answers2

2

getMouse() function return an instance of Point (http://mcsp.wartburg.edu/zelle/python/graphics.py), from which you can extract x and y. You can use for example :

p = win.getMouse()
x = p.x
y = p.y

I hope it helps :)

Thierry Lathuille
  • 23,663
  • 10
  • 44
  • 50
Nicolas M.
  • 1,472
  • 1
  • 13
  • 26
  • Thank You ! This worked for me, but now another problem has taken place. It says : AttributeError : type object 'Circle' has no attribute 'moveTo' –  May 21 '17 at 18:50
  • I checked in the module and I don't find any **moveTo()** function. There is a **move()** function but it takes a _dx_, _dy_ as input. That means you have to subtract the current position of the circle to your mouse position. – Nicolas M. May 21 '17 at 18:55
  • Does it mean there should be another module having 'moveTo()' ? If you find it, please share. Thanks ! –  May 21 '17 at 19:32
  • @Shivam `def moveTo(p, x, y): p.move(x - p.x, y - p.y)` – Frederik.L May 22 '17 at 00:56
  • Thank you, I had my exams so I didn't reply. –  Jun 09 '17 at 03:03
0

There is no moveTo() method and you don't need one, the move() method should do what you want if you make the movement relative to the object's center position:

from graphics import *

win = GraphWin("My Circle", 500, 500)

# ...

circle = Circle(Point(150, 150), 50)
circle.draw(win)

# ...

while True:
    point = win.getMouse()
    center = circle.getCenter()
    circle.move(point.x - center.x, point.y - center.y)
cdlane
  • 40,441
  • 5
  • 32
  • 81