import tkinter as tk
class MoveCanvas(tk.Canvas):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.dx = 2
self.dy = 10
self.box = self.create_rectangle(0, 0, 10, 10, fill="black")
line = self.create_line(10, 370, 2000, 370, fill="black")
self.dt = 25
self.tick()
def tick(self):
self.move(self.box, self.dx, self.dy)
self.after(self.dt, self.tick)
def change_heading(self, dx, dy):
self.dx = dx
self.dy = dy
if __name__ == "__main__":
root = tk.Tk()
root.geometry("300x300")
root.state("zoomed")
cvs = MoveCanvas(root)
cvs.pack(fill="both", expand=True)
ds = 3
root.bind("<KeyPress-a>", lambda _: cvs.change_heading(-ds, 0))
root.bind("<KeyPress-d>", lambda _: cvs.change_heading(ds, 0))
root.bind("<KeyPress-w>", lambda _: cvs.change_heading(0, -ds))
root.bind("<KeyPress-s>", lambda _: cvs.change_heading(0, ds))
root.mainloop()