Python 在这些情况下如何使用destroy

Python 在这些情况下如何使用destroy,python,python-3.x,tkinter,Python,Python 3.x,Tkinter,在处理模块时遇到另一个问题。我不能让“破坏”工作。 我想用一个按钮打开,用另一个按钮关闭顶层窗口 下面是一个应用销毁的小代码 # module uno.py import tkinter as tk class PRUEBA: def __init__(*args): ventana_principal = tk.Tk() ventana_principal.geometry ("600x600")

在处理模块时遇到另一个问题。我不能让“破坏”工作。 我想用一个按钮打开,用另一个按钮关闭顶层窗口

下面是一个应用销毁的小代码

# module uno.py

import tkinter as tk 
class PRUEBA:
    def __init__(*args):
    
        ventana_principal = tk.Tk()        
        ventana_principal.geometry ("600x600") 
        ventana_principal.config (bg="blue") 
        ventana_principal.title ("PANTALLA PRINCIPAL") 
    
        def importar():
        
            from dos import toplevel
            top = toplevel(ventana_principal)  

        boton = tk.Button (ventana_principal , text = "open" , command = importar)
        boton.pack ( )  
        boton1 = tk.Button (ventana_principal , text = "close" , command = top.destroy) #does not work destroy
        boton1.pack ( )
    
        ventana_principal.mainloop()  
PRUEBAS = PRUEBA ()

#module dos.py

import tkinter as tk 

class toplevel(tk.Toplevel):
     def __init__(self, parent, *args, **kw):
        super().__init__(parent, *args, **kw)
        self.geometry("150x40+190+100")        
        self.resizable(0, 0)
        self.transient(parent)

这是因为
top
importar()
函数中的局部变量

改用实例变量self.top:

class PRUEBA:
    def __init__(self, *args):
    
        ventana_principal = tk.Tk()        
        ventana_principal.geometry("600x600") 
        ventana_principal.config(bg="blue") 
        ventana_principal.title("PANTALLA PRINCIPAL") 
    
        def importar():
            from dos import toplevel
            self.top = toplevel(ventana_principal)  

        boton = tk.Button(ventana_principal, text="open", command=importar)
        boton.pack()  
        boton1 = tk.Button(ventana_principal, text="close", command=lambda: self.top.destroy())
        boton1.pack()

请注意,您需要满足以下情况:在单击
关闭
按钮之前,多次单击
打开
按钮。然后将有两个或多个
toplevel
窗口,
close
按钮只能关闭最后打开的窗口

您也不能在
打开
按钮之前单击
关闭
按钮