我是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很有用。可以使用圆形面片(http://matplotlib.org/api/patches_api.html)在绘图区域上绘制一个圆,然后在图形轴上单击鼠标时移动它。您将需要连接到事件单击侦听器并定义一个处理绘图更新的回调函数-有关如何执行此操作的示例,请参见http://matplotlib.org/users/event_handling.html。你可以使用鼠标和按键的方法来获得扩展数据的坐标。在
这对我在python 2.7中起到了作用:
假设您想继续使用最初使用的图形包,可以这样做,但缺少保存鼠标位置并将其与圆的中心位置进行比较的代码:
在一个实际的程序中,你需要关闭窗口来打破跟踪循环,你将把它作为设计的一部分来处理(例如,某些用户操作会导致
while True:
循环中的break
)相关问题 更多 >
编程相关推荐