0

When I run this code, I get an error that says UnboundLocalError: local variable 'rootent' referenced before assignment.

class calculator():
    def __init__(self):
        def options():
            fetch=float(rootent.get()) #Location of error
            if fetch=='1':
                def IEntry():
                    fetch=float(rootent.get())
                    fetch1=float(rootent1.get())
                    answer=fetch,'+',fetch1,'=',fetch1+fetch2
                    ansLabel=Label(root,text=answer).pack()
                root=Tk()
                root.title('Addition')
                root.geometry('450x450+200+200')
                rootlabel=Label(root,text='Enter first number').pack()
                rootent=Entry()
                rootent.pack()
                rootlabel1=Label(root,text='Enter second number').pack()
                rootent1=Entry()
                rootent1.pack()
                return
        root=Tk()
        root.title('Calculator Menu')
        root.geometry('450x450+200+200')
        rootlabel=Label(root,text='1.Addition').pack()
        rootlabel1=Label(root,text='2.Subtraction').pack()
        rootlabel2=Label(root,text='3.Multiplication').pack()
        rootlabel3=Label(root,text='4.Division').pack()
        rootent=Entry(root) #This is what i am trying to input into 'def options()'
        rootent.pack()
        rootbutton=Button(root,text='Enter option',command=options).pack()

I have tried making rootent global in the function, and I've tried passing it as a para with no luck.

What is wrong, and how do I fix it?

Karl Knechtel
  • 62,466
  • 11
  • 102
  • 153
user2993584
  • 139
  • 2
  • 5
  • 13
  • 1
    While this isn't directly related to the problem, tkinter isn't designed to have more than one instance of `Tk`. If you want to create an additional window you should create an instance of `Toplevel` – Bryan Oakley Mar 28 '14 at 12:37
  • Does this answer your question? [Is it possible to modify a variable in python that is in an outer (enclosing), but not global, scope?](https://stackoverflow.com/questions/8447947/is-it-possible-to-modify-a-variable-in-python-that-is-in-an-outer-enclosing-b) and [UnboundLocalError on local variable when reassigned after first use](https://stackoverflow.com/questions/370357/) – Karl Knechtel Sep 12 '22 at 10:49

3 Answers3

1

The problem is that python scoping rules are a bit strange. If a function has an assignment to a variable, that variable is assumed local to the function and python won't look in enclosing scopes. In your case, the offending line is rootent=Entry(). your call to rootent.get() is trying to access this rootent variable before it has been assigned. Since you are in python 3.x you can use the nonlocal declaration to make python access the outer scope. Just put nonlocal rootent at the beginning of options() and I think it will work correctly.

There is more discussion of the use of the nonlocal operator here: Python nonlocal statement

Basically, global tells python that the variable name in question resides at the module (file) level. nonlocal tells python to search enclosing scopes for the named variable and use that version, which is more like the behavior you get 'by default' in other languages where you have to explicitly declare all variables.

Community
  • 1
  • 1
Evan
  • 2,217
  • 15
  • 18
0

You are trying to get something from a variable that has not been previously declared. Where is the rootent variable declared in your code?. You need to show us that. It is hard to guess what the type of rootent is.

jramirez
  • 8,537
  • 7
  • 33
  • 46
  • I put a comment next to the 'rootent' that I am wanting to use in the def option(), 3rd line from the bottom – user2993584 Nov 16 '13 at 00:01
  • I can see that, but that variable can not be referenced before being set to some value ie. a dictionary or another object. Somewhere there needs to exist something like this `rootent = to_something` then you can use, well only if that object has function called `get()` – jramirez Nov 16 '13 at 00:04
-1

Thanks for the help, it's now fixed, added nonlocal and changed float(rootent.get()) to rootent.get() and it seemed to fix everything.

Alok
  • 2,629
  • 4
  • 28
  • 40
user2993584
  • 139
  • 2
  • 5
  • 13