Variable apparently not defined when it is
Question: Having an issue with a function I created in a separate file.

Here's my root program:

    
    
    #Import TKINTER toolset:
    from tkinter import *
    from mousexy import *
    
    #Starting variables:
    #Defining mouse x and y coordinates
    
    global mouse_x
    global mouse_y
    mouse_x = 0
    mouse_y = 0
    
    #Main window:
    window = Tk()
    window.title = ("Solomon's animation tool")
    
    #Workspace and Canvas:
    global wrkspace
    wrkspace =  Frame(window, bg="red",width=640,height=480)
    global canvas
    canvas = Canvas(wrkspace,bg="white",width=640,height=480)
    
    #Keyframe editor: (DO LATER)
    
    #Test for finding mouse xy
    canvas.bind("<Button-1>",find_mouse_xy)
    
    wrkspace.pack()
    canvas.pack()
    
    #Runs window:
    window.mainloop()
    

and here's my function in a separate file (mousexy.py)

    
    
    def find_mouse_xy(event):
        mouse_x = canvas.winfo_pointerx()
        mouse_y = canvas.winfo_pointery()
        print ("x: " + str(mouse_x))
        print ("y: " + str(mouse_y))
    

when I run my root program and click, the console tells me that `canvas` is
not defined when it clearly is, what am I doing wrong?

    
    
    mouse_x = canvas.winfo_pointerx()
    NameError: name 'canvas' is not defined
    Exception in Tkinter callback
    Traceback (most recent call last):
      File "C:\Python34\lib\tkinter\__init__.py", line 1487, in __call__
        return self.func(*args)
      File "C:\Users\SOLLUU\Documents\Python\Animation software\mousexy.py", line 2, in find_mouse_xy
        mouse_x = canvas.winfo_pointerx()
    NameError: name 'canvas' is not defined
    >>>
    


Answer: `find_mouse_xy` is looking for `mousexy.canvas`. You defined
`__main__.canvas`. They are two entirely separate variables.

What you probably want is

    
    
    def find_mouse_xy(event):
        # Coordinate of the mouse when the event occurred.
        mouse_x = event.x
        mouse_y = event.y
        # What object was clicked? This handler could
        # be attached to many different widgets in your program.
        where = event.widget
        # ...
    

