Tkinter WEIRDNESS or Python WEIRDNESS?

steve spambucket666au at yahoo.com.au
Sat Mar 12 00:06:05 EST 2005


In a nutshell, my problem is that I am getting this runtime error, and
I have no clue why.  Please bear in mind its my first python program:

"localvariable 'currentAbility' referenced before asignment"

in this function which is a callback for a button:

def nextThing():
    if lookingAtAbilities == 1:
        currentAbility = currentAbility + 1
        if currentAbility not in ability:
            currentAbility = 0
        clearScreen()
        ability[currentAbility].draw(currentAbility)
        drawMainButtons()
    else:
        currentCreature = currentCreature + 1
        if currentCreature not in creature:
            currentCreature = 0
        clearScreen()
        creature[currentCreature].draw(currentCreature)
        drawMainButtons()

now the thing is, both lookingAtAbilities  and  currentAbility are
global variables.  In fact, they are both assigned at the same part of
the program, right at the start.  yet lookingAtAbilities  is
recognized as a global by this function, and currentAbility is not,
generating this runtime error.

here is the structure of the code:

----------------------------------------
import string
import types
from Tkinter import *

# globals
lookingAtAbilities = 1
currentAbility = 2

(snip: some class and function definitions)

def nextThing():
    if lookingAtAbilities == 1:
        currentAbility = currentAbility + 1
        if currentAbility not in ability:
            currentAbility = 0
        clearScreen()
        ability[currentAbility].draw(currentAbility)
        drawMainButtons()
    else:
        currentCreature = currentCreature + 1
        if currentCreature not in creature:
            currentCreature = 0
        clearScreen()
        creature[currentCreature].draw(currentCreature)
        drawMainButtons()

def drawMainButtons():
    back = Button(win, text="prev", command=prevThing)
    back.grid(row=20, column=0)
    next = Button(win, text="next", command=nextThing)
    next.grid(row=20, column=1)

# START OF EXECUTION #

masterWin = Tk()
win = Frame(masterWin)
win.grid()

drawMainButtons()
masterWin.mainloop()   
------------------------------------------

This has me flumoxed

thanks if you can work it out,

Steve



More information about the Python-list mailing list