I am new to Python and was wondering how I could make an image change to another when a key is pressed. I want my image to change from GuyUp.gif to GuyDown.gif when I press the down arrow key so it looks like my guy is actually walking normally. My code in Python looks like this:
from tkinter import *
tk = Tk()
tk.title("Triangle Movement")
tk.resizable(0, 0)
canvas = Canvas(tk, width=500, height=500)
canvas.pack()
tk.update()
guyup = PhotoImage(file = 'GuyUp.gif')
canvas.create_image(5, 5, image = guyup, anchor = NW)
def movetriangle(event):
if event.keysym == 'Up':
canvas.move(1, 0, -4)
elif event.keysym == 'Down':
canvas.move(1, 0, 4)
elif event.keysym == 'Left':
canvas.move(1, -4, 0)
elif event.keysym == 'Right':
canvas.move(1, 4, 0)
elif event.keysym == 'w':
canvas.move(2, 0, -4)
elif event.keysym == 's':
canvas.move(2, 0, 4)
elif event.keysym == 'a':
canvas.move(2, -4, 0)
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)
I do have the two images and would like to put it in my elif statement with the keysym of 'Down' Thank you for your help!