Getting "UnboundLocalError: local variable 'location_y' referenced before assignment" even though it's not

Question:

So I’m trying to create a D&D map viewer (so if that I can make the map interactive) but for some reason I keep getting "UnboundLocalError: local variable ‘location_y’ referenced before assignment" and "UnboundLocalError: local variable ‘location_x’ referenced before assignment" and it doesn’t make any sense to me so what do I need to add or change to make this work?

Code:

import PIL.Image
from PIL import ImageTk
from tkinter import *

location_x = 0
location_y = 0
scroll_wheel = 1

root = Tk()
canvas = Canvas(root, width=1920, height=1080)
canvas.pack()

file_name = ("MAP_X" + str(location_x) + "_Y" + str(location_y) + ".png")

def onKeyPress(event):
    if(event.char == "a"):
        location_x -= 1
        if(location_x == -1):
            done_check_x = 0
            while(done_check_x == 0):
                if(PIL.Image.open("MAP_X" + str(location_x) + "_Y" + str(location_y) + ".png")):
                    location_x += 1
                else:
                    location_x -= 1
                    done_check_x = 1

    elif(event.char == "d"):
        location_x += 1
        if not (PIL.Image.open("MAP_X" + str(location_x) + "_Y" + str(location_y) + ".png")):
            location_x = 0

    elif(event.char == "w"):
        location_y -= 1
        if(location_y == -1):
            done_check_y = 0
            while(done_check_y == 0):
                if(PIL.Image.open("MAP_X" + str(location_x) + "_Y" + str(location_y) + ".png")):
                    location_y += 1
                else:
                    location_y -= 1
                    done_check_y = 1
    
    elif(event.char == "s"):
        location_y += 1
        if not (PIL.Image.open("MAP_X" + str(location_x) + "_Y" + str(location_y) + ".png")):
            location_y = 0

root.bind('<KeyPress>', onKeyPress)

while(root):
    file_name = ("MAP_X" + str(location_x) + "_Y" + str(location_y) + ".png")
    img = PIL.Image.open(file_name)
    resized_img = ImageTk.PhotoImage(img.resize((1920 * scroll_wheel, 1080 * scroll_wheel)))
    canvas.create_image(0,0, anchor=NW, image=resized_img)
    
    mainloop()

#with Image.open("MAP_X" + str(start_location_x) + "_Y" + str(start_location_y) + ".png") as img:

Thanks in advance!

Asked By: Sammyueru

||

Answers:

Change this:

def onKeyPress(event):
    if(event.char == "a"):

To this:

def onKeyPress(event):
    global location_x, location_y, scroll_wheel

    if(event.char == "a"):
Answered By: brunson

No, you actually try to reference a local variables location_x and location_y, which are not existent (so that’s why -= operator in you case raises this exception, you can’t modify variable that doesn’t yet exist).

In your code both location_x and location_y are global variables, and to modify them you have to explicitly note that. To do that, simply declare that you are working with global variables in your function like so:

x = 0
y = 0

def func():
    global x, y 
    x -= 1
    y -= 1
Answered By: langley
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.