正如建议的那样,您可以绘制从球的最后一个位置开始到当前新球位置结束的线条。
我在 tkinter 中制作了简单的应用程序,您可以根据需要进行修改。
它仅使用一个画布,您只需单击画布即可移动球。鼠标右键单击重置画布和球的位置。
from tkinter import Tk, Canvas, Frame, BOTH
class MovingBall(Frame):
ball_r = 25
x, y = 0, 0 # ball last coords
def __init__(self):
super().__init__()
self.ball = None
self.initUI()
def initUI(self):
self.master.title("Moving Ball")
self.pack(fill=BOTH, expand=1)
self.canvas = Canvas(self)
# self.canvas.pack(fill=BOTH, expand=1)
self.ball = self.paint_ball(self.x, self.y)
self.canvas.pack(fill=BOTH, expand=1)
# Left click on canvas moves the ball
self.canvas.bind("<Button-1>", self.move)
# Right click on canvas reset ball position
self.canvas.bind("<Button-3>", self.reset)
def paint_ball(self, x, y):
return self.canvas.create_oval(x - self.ball_r, y - self.ball_r, x + self.ball_r, y + self.ball_r, fill="red",
outline="silver", width=1)
def paint_path(self, x, y):
return self.canvas.create_line(self.x, self.y, x, y, fill="silver", width=1)
def move(self, event):
# Remove last painted ball
self.canvas.delete(self.ball)
# Add new line to the path
self.paint_path(event.x, event.y)
# Paint new ball at new position
self.ball = self.paint_ball(event.x, event.y)
self.canvas.pack(fill=BOTH, expand=1)
# Store current ball coords
self.x, self.y = event.x, event.y
def reset(self, event):
"""Reset whole scene and put ball to 0,0"""
self.x, self.y = 0, 0
self.canvas.delete("all")
self.ball = self.paint_ball(self.x, self.y)
self.canvas.pack(fill=BOTH, expand=1)
def main():
root = Tk()
MovingBall()
root.geometry("1024x768")
root.mainloop()
if __name__ == '__main__':
main()