2011-11-04 13 views
10
import tkinter 


class App(): 
    def __init__(self): 
     self.root = Tkinter.Tk() 
     button = Tkinter.Button(self.root, text = 'root quit', command=self.quit) 
     button.pack() 
     self.root.mainloop() 

    def quit(self): 
     self.root.destroy 

app = App() 

Come posso rendere la mia funzione quit per chiudere la finestra?Funzione per chiudere la finestra in Tkinter

risposta

38
def quit(self): 
    self.root.destroy() 

Aggiungere parentesi dopo destroy per chiamare il metodo.

Quando si utilizza command=self.root.destroy si passa il metodo per Tkinter.Buttonsenza le parentesi perché si vuole Tkinter.Button per memorizzare il metodo per il futuro di chiamata, di non chiamare immediatamente quando viene creato il pulsante.

Tuttavia, quando si definisce il metodo quit, è necessario chiamare il numero self.root.destroy() nel corpo del metodo perché a questo punto è stato chiamato il metodo.

+0

+1 per la spiegazione delle parentesi, proprio quello che stavo cercando! – AndreasT

1
class App(): 
    def __init__(self): 
     self.root = Tkinter.Tk() 
     button = Tkinter.Button(self.root, text = 'root quit', command=self.quit) 
     button.pack() 
     self.root.mainloop() 

    def quit(self): 
     self.root.destroy() 

app = App() 
0
def exit(self): 
    self.frame.destroy() 
exit_btn=Button(self.frame,text='Exit',command=self.exit,activebackground='grey',activeforeground='#AB78F1',bg='#58F0AB',highlightcolor='red',padx='10px',pady='3px') 
exit_btn.place(relx=0.45,rely=0.35) 

questo ha lavorato per me per distruggere il mio telaio Tkinter sul clic sul pulsante di uscita.

Problemi correlati