我使用了Zelle" graphics.py"文件。我用Thonny。我想要 使用函数" 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'
答案 0 :(得分:2)
getMouse()函数返回Point(http://mcsp.wartburg.edu/zelle/python/graphics.py)的实例,从中可以提取x和y。您可以使用例如:
p = win.getMouse()
x = p.x
y = p.y
我希望它有所帮助:)
答案 1 :(得分: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)