2011-11-04 6 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() 

विंडो को बंद करने के लिए मैं अपना quit फ़ंक्शन कैसे बना सकता हूं?टिंकर

उत्तर

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

विधि को कॉल करने के लिए destroy के बाद कोष्ठक जोड़ें।

जब आप का उपयोग command=self.root.destroy आप Tkinter.Buttonको विधि के बिना कोष्ठकों क्योंकि आप Tkinter.Button भविष्य फोन करने के लिए विधि संग्रहीत करना चाहते हैं इसे तुरंत कॉल करने के लिए नहीं है जब बटन बनाई गई है गुजरती हैं,।

लेकिन जब आप quit विधि को परिभाषित करते हैं, तो आपको विधि के शरीर में self.root.destroy() पर कॉल करने की आवश्यकता होती है क्योंकि तब तक विधि को बुलाया जाता है।

+0

+1 कोष्ठक स्पष्टीकरण के लिए +1, बस जो मैं खोज रहा था! – 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) 

यह मेरे लिए काम किया बाहर निकलें बटन क्लिक करने पर मेरी Tkinter फ्रेम नष्ट करने के लिए।