Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!python3
- from tkinter import *
- tk = Tk()
- tk.title("Triangle Movement")
- tk.resizable(0, 0)
- canvas = Canvas(tk, width=500, height=500)
- canvas.pack()
- tk.update()
- current_image = PhotoImage(file='1.pnm')
- image_id = canvas.create_image(5, 5, image=current_image, anchor=NW)
- other_image = PhotoImage(file='2.pnm')
- def movetriangle(event):
- if event.keysym == 'Up':
- print("Up")
- canvas.move(image_id, 0, -4)
- elif event.keysym == 'Down':
- print("Down")
- canvas.move(image_id, 0, 4)
- swap_images()
- elif event.keysym == 'Left':
- print("Left")
- canvas.move(image_id, -4, 0)
- elif event.keysym == 'Right':
- print("Right")
- canvas.move(image_id, 4, 0)
- elif event.keysym == 'w':
- print("w")
- canvas.move(2, 0, -4)
- elif event.keysym == 's':
- print("s")
- canvas.move(2, 0, 4)
- elif event.keysym == 'a':
- print("a")
- canvas.move(2, -4, 0)
- else:
- print("else")
- canvas.move(2, 4, 0)
- canvas.bind_all('<KeyPress-Up>', movetriangle)
- canvas.bind_all('<KeyPress-Down>', movetriangle)
- canvas.bind_all('<KeyPress-Left>', movetriangle)
- canvas.bind_all('<KeyPress-Right>', movetriangle)
- canvas.bind_all('<KeyPress-w>', movetriangle)
- canvas.bind_all('<KeyPress-s>', movetriangle)
- canvas.bind_all('<KeyPress-a>', movetriangle)
- canvas.bind_all('<KeyPress-d>', movetriangle)
- def swap_images():
- global current_image, other_image, image_id
- x, y = canvas.coords(image_id)
- canvas.delete(image_id)
- image_id = canvas.create_image(x, y, image=other_image, anchor=NW)
- current_image, other_image = other_image, current_image
- tk.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement