我不能破坏顶层(Tkinter,python)
在我的节目里
1)开始时,用户按下按钮,顶层显示
2)在顶层有更多的小部件和一个按钮
3)当用户按下此(第二个)按钮时,功能(名称_顶层破坏())开始工作
4)但终端写我“NameError:全局名称'name_of_toplevel'未定义”
5)但它真的被定义了!在
6)按钮用“bind”方法与函数绑定
程序文本:
from Tkinter import *
def Begin(event):
okno.destroy()
def QuitAll(event):
exit(0)
def OpenOkno(event):
#print "<ButtonRelease-1> really works! Horray!"
okno = Toplevel()
okno.title('Question')
okno.geometry('700x300')
Sign = Label(okno,text = 'Quit the program?', font = 'Arial 17')
Sign.grid(row = 2, column = 3)
OK = Button(okno, text = 'YES', bg = 'yellow', fg = 'blue', font = 'Arial 17')
OK.grid(row = 4, column = 2)
OK.bind("<ButtonRelease-1>",QuitAll)
NO = Button(okno, text = 'NO', bg = 'yellow', fg = 'blue', font = 'Arial 17')
NO.grid(row = 4, column = 4)
NO.bind("<ButtonRelease-1>",Begin)
root = Tk() # main window 'program_on_Python'
root.title('Program_on_Python')
root.geometry('400x600')
knpk = Button(root, text = 'click here!', width = 30, height = 5, bg = 'yellow', fg = 'blue', font = 'Arial 17')
knpk.grid(row = 2, column = 2)
knpk.bind("<ButtonRelease-1>",OpenOkno)
root.mainloop()
如果可以的话,请帮帮我
okno
不存在于OpenOkno
函数之外,因此尝试在其他任何地方访问它将导致NameError
。解决这个问题的一种方法是将Begin
移到OpenOkno
内,其中okno
对象是可见的。在也可以使用lambda表达式代替完整函数,作为
^{pr2}$Bind
的参数。在{cd1>也可以是全局变量。然后,您需要在需要分配给okno的任何地方使用
global okno
语句。在相关问题 更多 >
编程相关推荐