How to apply a destroy () to a Top Level window?

1

I want to make my top level window close when I press the "Close" button that is located inside the top level itself. Could you tell me what's wrong?

My code is as follows:

 def win2 ():
  t1 = Toplevel(bg="Brown")
  t1.title("Modificar Datos")
  t1.geometry('500x300')
  t1.focus_set()
  t1.grab_set()
  t1.transient(master=ventana)



  inf=StringVar()
  t2=Entry(t1,textvariable=inf)
  t2.grid(row=5,column=1,pady=20)

  t2l = Label(t1,text='Ingrese dato',bg="Cyan2")
  t2l.grid(row=4, column=1,padx=100,pady=10, sticky = N)

  wb = Frame(t1, width = 15, height = 10)
  b1 = Button(wb, text = "Cerrar", bg = "SkyBlue", command = salir2)
  b1.pack()
  wb.grid(column = 1, row = 6)

This function creates the toplevel window together with a label, an entry and a button, the button sends to call the following function:

def salir2():
  t1.destroy()

According to me the correct instance is t1 because it is the toplevel but when running it tells me that t1 is not defined.

Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Python27\lib\lib-tk\Tkinter.py", line 1537, in __call__
return self.func(*args)
File "C:\Users\efrias002\Desktop\Python\SQL Tkinter v2.py", line 139, in  salir2
t1.destroy()
NameError: global name 't1' is not defined
    
asked by Erick Finn 19.01.2018 в 23:09
source

2 answers

0

t1 is not defined because it is a local variable that only exists within your function win2 , therefore salir2 can not access it.

The simplest thing in your case is that you pass t1.destroy as a callback directly:

b1 = Button(wb, text="Cerrar", bg="SkyBlue", command=t1.destroy)

There are other possibilities but they do not make much sense just for this, like making t1 be a global variable:

from Tkinter import *

def win2():
    global t1
    t1 = Toplevel(bg="Brown")
    t1.title("Modificar Datos")
    t1.geometry('500x300')
    t1.focus_set()
    t1.grab_set()
    t1.transient(master=ventana)

    inf = StringVar()
    t2 = Entry(t1, textvariable=inf)
    t2.grid(row=5, column=1, pady=20)

    t2l = Label(t1,text='Ingrese dato', bg="Cyan2")
    t2l.grid(row=4, column=1, padx=100, pady=10, sticky=N)

    wb = Frame(t1, width=15, height=10)
    b1 = Button(wb, text="Cerrar", bg="SkyBlue", command=salir2)
    b1.pack()
    wb.grid(column=1, row=6)


def salir2():
    t1.destroy()

ventana = Tk()
t1 = None
Button(ventana, text="Abrir TopLevel", command=win2).pack()
ventana.mainloop()

or define salir2 within win2 :

def win2():

    def salir2():
        t1.destroy()

    t1 = Toplevel(bg="Brown")
    t1.title("Modificar Datos")
    t1.geometry('500x300')
    t1.focus_set()
    t1.grab_set()
    t1.transient(master=ventana)

    inf = StringVar()
    t2 = Entry(t1, textvariable=inf)
    t2.grid(row=5, column=1, pady=20)

    t2l = Label(t1,text='Ingrese dato', bg="Cyan2")
    t2l.grid(row=4, column=1, padx=100, pady=10, sticky=N)

    wb = Frame(t1, width=15, height=10)
    b1 = Button(wb, text="Cerrar", bg="SkyBlue", command=salir2)
    b1.pack()
    wb.grid(column=1, row=6)

If your app is moderately complex, consider using object-oriented programming, it will make life much easier.

    
answered by 19.01.2018 / 23:20
source
0

It's because you're defining the variable within a function, and the other functions do not detect it by this

    
answered by 19.01.2018 в 23:19