简体   繁体   English

函数内的全局变量

[英]global variable within a function

i am trying to tell one function to remove the button lb from the screen using place.forget() but i am getting an error because i have defined the button lb as a global variable within another function and so each time it tries to recall it it doesn't 'see' the global variable. 我试图告诉一个函数使用place.forget()从屏幕上删除按钮lb,但是由于出现了错误,因为我已将按钮lb定义为另一个函数中的全局变量,因此每次尝试调用它时它没有“看到”全局变量。 I know why it's giving me the error i'm just unsure of how to correct it, any help would be greatly appreciated. 我知道为什么它会给我错误,但我不确定如何纠正它,任何帮助将不胜感激。

def page4():
    master.title('Page 4')
    #draw
    c19.grid(pady=slpady)
    c19.grid(row=sly,column=slx1)
    c20.grid(row=sly,column=slx2)
    c21.grid(row=sly,column=slx3)
    c22.grid(row=sly,column=slx4)
    c23.grid(row=sly,column=slx5)
    c24.grid(row=sly,column=slx6)
    rec.grid(row=rec_y, column=rec_x,columnspan=3)
    clear_4.grid(row=clear_y, column=clear_x,columnspan=3)
    #hide
    c1.grid_remove()
    c2.grid_remove()
    c3.grid_remove()
    c4.grid_remove()
    c5.grid_remove()
    c6.grid_remove()
    c7.grid_remove()
    c8.grid_remove()
    c9.grid_remove()
    c10.grid_remove()
    c11.grid_remove()
    c12.grid_remove()
    c13.grid_remove()
    c14.grid_remove()
    c15.grid_remove()
    c16.grid_remove()
    c17.grid_remove()
    c18.grid_remove()
    new_show.grid_remove()
    save_show.grid_remove()
    load_show.grid_remove()
    pre_1.grid_remove()
    pre_2.grid_remove()
    pre_3.grid_remove()
    pre_4.grid_remove()
    clear_1.grid_remove()
    clear_2.grid_remove()
    clear_3.grid_remove()
    lb.place_forget()

#Presets
def presets():
    master.title('Presets')
    pre_1.grid(row=1,column=1,padx=ppadx, pady=ppady)
    pre_2.grid(row=1,column=2,padx=ppadx, pady=ppady)
    pre_3.grid(row=2,column=1,padx=ppadx, pady=ppady)
    pre_4.grid(row=2,column=2,padx=ppadx, pady=ppady)
    global lb
    lb=Button(master,width=20,height=5,text='LOCK',bg='burlywood1',command=lockscreen)
    lb.place(x=450,y=580)

the error im getting is: 我得到的错误是:

Exception in Tkinter callback Traceback (most recent call last):
    File "C:\Python27\lib\lib-tk\Tkinter.py", line 1470, in __call__
        return self.func(*args)
    File "C:\Users\Josh Bailey\Desktop\pi_dmx\pi_dmx512py2.py", line 412, in page4
        lb.place_forget() NameError: global name 'lb' is not defined

在启动任何功能之前,请初始化lb。

I have solved it by moving the global lb and the lb=Button(master,width=20,height=5,text='LOCK',bg='burlywood1',command=lockscreen) to the top of my code so that it was out of any function and then had to rearrange the rest of my code so that the function being called by that button ( lockscreen) sat above the preset function. 我已经通过将全局lb和lb = Button(master,width = 20,height = 5,text ='LOCK',bg ='burlywood1',command = lockscreen)移到我的代码的顶部来解决了它没有任何功能,然后必须重新排列我的代码的其余部分,以使该按钮(锁屏)所调用的功能位于预设功能的上方。 Thanks for the help! 谢谢您的帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM