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!
>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"):