分配之前引用的UnboundLocalError本地变量

时间:2022-02-08 22:47:04

Every time I run this code so far, I get this error:

每次我运行这个代码到目前为止,我都会得到这个错误:

Error message: 'UnboundLocalError: local variable 'rootent' referenced before assignment'

Here is the code i've been running: I have tried making rootent global in the function, and i've tried passing it as a para with no luck. (would prefer a clear answer/explanation since i'm not that great a programmer(might not understand your answer))

下面是我一直在运行的代码:我尝试在函数中使rootent全局化,并尝试将其作为para传递,但运气不佳。(因为我不是很优秀的程序员,所以我更希望得到一个清晰的答案/解释(可能不理解你的答案)

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()

3 个解决方案

#1


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.

问题是python的范围规则有点奇怪。如果一个函数赋值给一个变量,那么这个变量就被假定为函数的局部变量,python不会在封闭作用域中查找。在您的示例中,违规行是rootent=Entry()。对root .get()的调用尝试在分配这个rootent变量之前访问它。因为您使用的是python 3。可以使用非本地声明使python访问外部范围。只需将非本地rootent放在选项()的开头,我认为它将正常工作。

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

这里有更多关于使用非本地操作符的讨论:Python非本地语句

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.

基本上,全局变量告诉python所讨论的变量名位于模块(文件)级别。nonlocal告诉python要为命名变量搜索封闭作用域并使用该版本,这更类似于在其他语言中必须显式声明所有变量的“默认”行为。

#2


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.

您正在尝试从一个尚未声明的变量中获取一些东西。在代码中声明的rootent变量在哪里?你得给我们看看。很难猜出根的类型是什么。

#3


-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.

感谢您的帮助,现在已经修复了它,添加了非本地的,并将float(root .get())更改为root .get(),它似乎修复了一切。

#1


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.

问题是python的范围规则有点奇怪。如果一个函数赋值给一个变量,那么这个变量就被假定为函数的局部变量,python不会在封闭作用域中查找。在您的示例中,违规行是rootent=Entry()。对root .get()的调用尝试在分配这个rootent变量之前访问它。因为您使用的是python 3。可以使用非本地声明使python访问外部范围。只需将非本地rootent放在选项()的开头,我认为它将正常工作。

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

这里有更多关于使用非本地操作符的讨论:Python非本地语句

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.

基本上,全局变量告诉python所讨论的变量名位于模块(文件)级别。nonlocal告诉python要为命名变量搜索封闭作用域并使用该版本,这更类似于在其他语言中必须显式声明所有变量的“默认”行为。

#2


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.

您正在尝试从一个尚未声明的变量中获取一些东西。在代码中声明的rootent变量在哪里?你得给我们看看。很难猜出根的类型是什么。

#3


-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.

感谢您的帮助,现在已经修复了它,添加了非本地的,并将float(root .get())更改为root .get(),它似乎修复了一切。