Error on Tkinter import
Question: I'm writing a Tkinter app in Python 2.7, but I'm running into some troubles
that I haven't had before. From what I can tell, it looks like the Tkinter
module is getting imported for the `__init__` function in my class, but not
for the other functions. Here's a simplified version of what I've got:

    
    
    from Tkinter import *
    
    class App:
        def __init__(self):
            self.master = Tk()
            self.window = Frame(self.master)
            self.window.grid()
    
            self.BuildFrames()
            self.master.mainloop()
    
        def BuildFrames(self):
            frames = []
            frames.append(Frame(self.window,borderwidth=2,padx=10,pady=10))
            # more code follows...
    
            for Frame in frames:
                Frame.grid()
    
    
    App()
    

When I run this, I get the following error:

    
    
    Traceback (most recent call last):
      File "myApp.py", line 131, in <module>
        App()
      File "myApp.py", line 12, in __init__
        self.BuildFrames()
      File "myApp.py", line 26, in BuildFrames
        frame1 = Frame(self.window,borderwidth=2,padx=10,pady=10)
    UnboundLocalError: local variable 'Frame' referenced before assignment
    

From what I can tell, the `Frame` function isn't being recognized as a Tkinter
method within the `BuildFrames()` function. How on earth could it be
recognized in `__init__` but not within `BuildFrames`???

I can fix the problem by changing the import to:

    
    
    import Tkinter as Tk
    

and then adding a `Tk.` in front of all the Tkinter methods, but would rather
avoid it (and I shouldn't have to do this anyway!)

I must be missing something big about the way the import works, but I could
swear this same type of code has worked for me before. Can someone help me out
with this one?


Answer: Maybe somewhere in your code, `Frame = ...` exists. Rename that variable to
other than `Frame`.

You are doing something like following code:

    
    
    >>> def f():
    ...     a + 1
    ...     a = 0
    ... 
    >>> f()
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
      File "<stdin>", line 2, in f
    UnboundLocalError: local variable 'a' referenced before assignment
    

[Why am I getting an UnboundLocalError when the variable has a
value?](http://docs.python.org/2/faq/programming.html#why-am-i-getting-an-
unboundlocalerror-when-the-variable-has-a-value)

**EDIT**

Change your BuildFrames as follow:

    
    
    def BuildFrames(self):
        frames = []
        frames.append(Frame(self.window,borderwidth=2,padx=10,pady=10))
        # more code follows...
    
        for frame in frames:
            frame.grid()
    

