Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

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!

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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"):
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading