Tkinter बटन का उपयोग करके Python से बाहर निकलने के लिए, आप नीचे दिए गए चरणों का पालन कर सकते हैं:
चरण
Tkinter लाइब्रेरी इम्पोर्ट करें और Tkinter फ्रेम का एक इंस्टेंस बनाएं।
geometry मेथड का उपयोग करके फ्रेम का आकार सेट करें।
विंडो बंद करने के लिए close() फ़ंक्शन परिभाषित करें। close() के अंदर win.destroy() मेथड कॉल करें।
अगला, एक बटन बनाएं और close() फ़ंक्शन कॉल करें।
अंत में, एप्लिकेशन विंडो का mainloop चलाएं।
उदाहरण
# Import the library from tkinter import * # Create an instance of window win = Tk() # Set the geometry of the window win.geometry("700x350") # Title of the window win.title("Click the Button to Close the Window") # Define a function to close the window def close(): #win.destroy() win.quit() # Create a Button to call close() Button(win, text= "Close the Window", font=("Calibri",14,"bold"), command=close).pack(pady=20) win.mainloop()आउटपुट
निष्पादन पर, यह निम्नलिखित आउटपुट देगा:

बटन पर क्लिक करने पर, यह विंडो बंद कर देगा।
win.destroy() के बजाय, आप एप्लिकेशन को बंद करने के लिए win.quit() का भी उपयोग कर सकते हैं। हालांकि, दोनों में एक सूक्ष्म अंतर है। win.quit() अचानक एप्लिकेशन को छोड़ देता है जिसका अर्थ है कि mainloop अभी भी बैकग्राउंड में चल रहा होगा। दूसरी ओर win.destroy() mainloop को समाप्त करता है और विंडो के अंदर सभी विजेट्स को नष्ट कर देता है।