Click here to Skip to main content
15,886,689 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I have encountered a problem whilst creating a changing animation in python 3, tkinter. I was trying to create a changing player gif animation when I found that the animation was not changing when I clicked the different keys that were previously defined. The error is that frames is not defined and I do not know how to define frames without it interfering

Here is my code:

Python
from tkinter import *
import os
import time

root = Tk()

LEFT_CHAR = input('LEFT BUTTON: ')
RIGHT_CHAR = input('RIGHT BUTTON: ')
UP_CHAR = input('UP BUTTON: ')
DOWN_CHAR = input('DOWN BUTTON: ')
ATK_CHAR = input('ATTACK BUTTON: ')

movespeed = 20

canvasH = 900
canvasW = 1200

animspdIDLE = 300
animspdATK = 300
animspdLEFT = 300
animspdRIGHT = 300
animspdUP = 300
animspdDOWN = 300

framerangeIDLE = 2
framerangeATK = 2
framerangeLEFT = 2
framerangeRIGHT = 2
framerangeUP = 2
framerangeDOWN = 2

def animcontrollermethod(event):

    if event.char == LEFT_CHAR:
        frames = [PhotoImage(file =(os.path.expanduser("~/Desktop/DaQueenLEFT.gif")),format = 'gif -index %i' % (i)) for i in range(framerangeLEFT)]
        currentframelength = framerangeLEFT
        animspdcurrent = animspdLEFT

    elif event.char == RIGHT_CHAR:
        frames = [PhotoImage(file=(os.path.expanduser("~/Desktop/DaQueenRIGHT.gif")),format = 'gif -index %i' % (i)) for i in range(framerangeRIGHT)]
        currentframelength = framerangeRIGHT
        animspdcurrent = animspdRIGHT

    elif event.char == UP_CHAR:
        frames = [PhotoImage(file=(os.path.expanduser("~/Desktop/DaQueenUP.gif")),format = 'gif -index %i' (i)) for i in range(framerangeUP)]
        currentframelength = framerangeUP
        animspdcurrent = animspdUP

    elif event.char == DOWN_CHAR:
        frames = [PhotoImage(file=(os.path.expanduser("~/Desktop/DaQueenDOWN.gif")),format = 'gif -index %i'(i)) for i in range(framerangeDOWN)]
        currentframelength = framerangeDOWN
        animspdcurrent = animspdDOWN

    elif event.char == ATK_CHAR:
        frames = [PhotoImage(file=(os.path.expanduser("~/Desktop/DaQueenATK.gif")),format = 'gif -index %i' % (i)) for i in range(framerangeATK)]
        currentframelength = framerangeATK
        animspdcurrent = animspdATK

    else:
        frames = [PhotoImage(file=(os.path.expanduser("~/Desktop/DaQueenIDLE.gif")),format = 'gif -index %i'% (i)) for i in range(framerangeIDLE)]
        currentframelength = framerangeIDLE
        animspdcurrent = animspdIDLE

def move(event):
    if event.char == UP_CHAR:
        c.move(character, 0, -movespeed)
    elif event.char == DOWN_CHAR:
        c.move(character, 0, movespeed)
    elif event.char == LEFT_CHAR:
        c.move(character, -movespeed, 0)
    elif event.char == RIGHT_CHAR:
        c.move(character, movespeed, 0)

c = Canvas(root,width = canvasW, height = canvasH)

c.bind("<KeyPress>",animcontrollermethod)
c.bind('<KeyPress>',move)

c.pack()
c.focus_set()

def update(ind):
    frame = frames[ind]
    ind += 1

    if ind >= currentframelength:
        ind = 0

    c.itemconfig(character, image=frame)
    root.after(animspdcurrent, update, ind)

character = c.create_image((canvasW/2,canvasH/2), image=frames[0])

root.after(0, update, 0)
root.update()

root.mainloop()

EDIT: This code raises an ERROR saying frames is not defined. How can I define frames without it interfering and showing a single animation? My goal is to create a script so that when I press the certain key the animation changes based on what I pressed and moves in the appropriate direction.

Thank You for everything!

I have created this question on "StackOverflow"
but no one has yet to reply. To who is interested here is the link:

python - Creating a changing an image/gif in tkinter that can move using PhotoImage (without PIL) [UNANSWERED] - Stack Overflow[^]



What I have tried:

I have tried debugging my code I have spent 2 days trying to figure out what isn't working, checked the file names and get around a solution that works. What I thought was a plausible solution never seems to be a solution so I turned to the internet to aid me.
Posted
Updated 7-Mar-20 20:00pm

1 solution

You haven't set the frames list in the
global environment. To do that,

def animcontrollermethod(event):
global frames
..
..
# your code
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900