2

我使用了 Zelle 的“graphics.py”文件。我用托尼。我想使用函数“getMouse() & getMouseNow()”,但出现了这些消息。我应该怎么办 ?帮我 !

代码 :

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

输出 :

在其中创建一个具有上述尺寸的窗口和一个圆。点击进入窗口后...

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'
4

2 回答 2

2

getMouse() 函数返回一个 Point ( http://mcsp.wartburg.edu/zelle/python/graphics.py ) 的实例,您可以从中提取 x 和 y。您可以使用例如:

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

我希望它有帮助:)

于 2017-05-21T18:12:54.747 回答
0

没有moveTo()方法,您也不需要,move()如果您相对于对象的中心位置进行移动,该方法应该做您想做的事情:

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)
于 2017-05-22T00:48:03.110 回答