我收到“ num”的UnboundLocalError,但我不知道为什么

Puffthemagicdragon01

我正在使用Tkinter来创建游戏,但遇到第一个窗口时,却收到此错误UnboundLocalError:在赋值之前引用了局部变量'num',尽管我已经将num设置为全局变量。我本来想通过函数来​​实现它,但是tkinter不允许我这样做,并且给我一个错误。

 from tkinter import *

 global num
 num = 1.0



 def situation_1_1():
    if num == 1.0:
       window10.destroy()
       num = 1.1


   global window11
   window11 = Tk()
   window11.title( " " )
   window11.resizable( 0, 0 )

   img1 = PhotoImage( file = "img_1_0.png" )



   Img_1 = Label( window11, image = img1)

   Label_1 = Label( window11, relief = "groove", width = 50 )

   Btn_1 = Button( window11, text = "Look around", command = situation_1_1)
   Btn_2 = Button( window11, text = "Go out front", command = situation_1_2)



   Img_1.grid( row = 1, column = 1, rowspan = 75, columnspan = 75 )

   Label_1.grid( row = 1, column = 76, rowspan = 50, columnspan = 100, padx = ( 10, 10 ) )

   Btn_1.grid( row = 61, column = 76, columnspan = 50 )
   Btn_2.grid( row = 61, column = 126, columnspan = 50 )



   Label_1.configure( text = """ """ )

   window11.mainloop()



def situation_1_0(num):
   num = 1.0
   global window10
   window10 = Tk()
   window10.title( " " )
   window10.resizable( 0, 0 )

   img1 = PhotoImage( file = "img_1_0.png" )



   Img_1 = Label( window10, image = img1)

   Label_1 = Label( window10, relief = "groove", width = 50 )

   Btn_1 = Button( window10, text = "Explore the house", command = situation_1_1)
   Btn_2 = Button( window10, text = "Go round back", command = situation_1_2)



   Img_1.grid( row = 1, column = 1, rowspan = 75, columnspan = 75 )

   Label_1.grid( row = 1, column = 76, rowspan = 50, columnspan = 100, padx = ( 10, 10 ) )

   Btn_1.grid( row = 61, column = 76, columnspan = 50 )
   Btn_2.grid( row = 61, column = 126, columnspan = 50 )


   Label_1.configure( text = """ """)

   window10.mainloop()


situation_1_0(num)
萨钦

当您尝试为外部作用域中的变量分配新值时,您需要在函数内添加global关键字。

在上面的示例中,当您将num传递给情况_1_0(..)函数时,num将被视为局部变量。在情境_1_0()中,您定义了对另一个函数情境_1_1()的调用,该函数试图为全局变量分配新值,因此会出现错误:local variable 'x' referenced before assignment在函数场景_1_1()中使用全局变量应该可以解决您的错误

您可以通过以下示例示例进行检查:

global num
num = 1.0

def bar():
    print(locals())
    global num
    if num == 1.0:
        num = 1.4
        print('num value withing bar fn: ', num)
# function to perform addition 
def foo(num):
    print(locals())
    bar()    
    print('num value within foo fn: ', num) 

# calling a function 
foo(num)
print('global num value: ', num)

locals()和globals()词典可以帮助查看存在哪些变量

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

我不断收到此错误消息,但我不知道为什么

我收到 mysqli 错误 1064 但我不知道为什么

我在 CSS 中不断收到此错误,但我不知道为什么

我不断收到 SyntaxError 但我不知道为什么

不知道为什么我收到 StopIteration 错误

这是我的最终代码。我最终多次收到错误消息,但我不知道为什么

收到int错误,但我不知道为什么。我的文件没有多余的空格

我不知道为什么,但我收到错误:“AttributeError: 'super' 对象没有属性 '__getattr__'”

我的查询在wordpress中返回null,但我不知道为什么

导航菜单是透明的,但我不知道为什么。我要背景

我的查询在wordpress中返回null,但我不知道为什么

我的主要功能是无限循环重复,但我不知道为什么

我遇到内存泄漏,但我不知道为什么

我的准确度是0.0,但我不知道为什么?

我的Matlab代码不起作用,但我不知道为什么?

我的应用程序崩溃了,但我不知道为什么?

我的质数函数坏了,但我不知道为什么

我尝试测试用Go语言编写的代码,但我不断收到此错误,但我不知道为什么

变异观察者单元测试失败,但我不知道为什么

引起异常org.springframework.beans.NotWritablePropertyException但我不知道为什么

导致单元测试失败的“如果”语句,但我不知道为什么

在 F# 中映射自动排序,但我不知道为什么

Hibernate Validator 不起作用,但我不知道为什么

“参数不匹配的关闭调用”,但我不知道为什么

渲染一无所获,但我不知道为什么

Python:插入排序逻辑失败,但我不知道为什么

无效的钩子调用。但我不知道为什么

反应类型错误,useState 不起作用。但我不知道为什么

为什么我收到这个错误?似乎我在 s1.grade_level 中有错误,但我不知道为什么