Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • Turtle
  • Python PIL
  • Python Program
  • Python Projects
  • Python DataBase
  • Python Flask
  • Python Django
  • Numpy
  • Pandas
  • Python Database
  • Data Science With Python
  • Machine Learning with Python
  • Django
  • Flask
  • R
Open In App
Next Article:
How to show webcam in TkInter Window - Python
Next article icon

How To Get Rid Of Python Tkinter Root Window?

Last Updated : 12 Jun, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

Python's Tkinter library offers a simple and effective way to create graphical user interfaces (GUIs) for your applications. However, when using Tkinter, you may encounter situations where you need to close or remove the root window, which is the main window of your GUI. Whether you're looking to clean up your interface or transition to another window, knowing how to properly get rid of the Tkinter root window is essential. In this guide, we'll explore methods to achieve this efficiently.

Get Rid Of Python Tkinter Root Window

Below are some of the ways by which we can get rid of Python Tkinter root window in Python:

Example 1: Using the destroy() Method

In this example, we create a Tkinter root window and define a function close_window() that calls the destroy() method on the root window object. This method terminates the main loop and destroys all widgets contained within the root window, effectively closing the window and ending the application.

Python
import tkinter as tk   def close_window():     root.destroy()   root = tk.Tk() root.title("Root Window")  # Create widgets and layout here  close_button = tk.Button(root, text="Close Window", command=close_window) close_button.pack()  root.mainloop() 

Output:

3

Example 2: Customizing Close Behavior

In this example, we use the protocol() method to customize the behavior when the user attempts to close the window using the window manager's close button (e.g., clicking the "X" button on the window). We define the on_closing() function to ask the user for confirmation before closing the window. If the user confirms, the root window is destroyed.

Python
import tkinter as tk   def on_closing():     if tk.messagebox.askokcancel("Quit", "Do you want to quit?"):         root.destroy()   root = tk.Tk() root.title("Root Window")  # Create widgets and layout here  root.protocol("WM_DELETE_WINDOW", on_closing)  root.mainloop() 

Output:

7

Next Article
How to show webcam in TkInter Window - Python

R

rs736tjxi
Improve
Article Tags :
  • Python
  • Python-tkinter
Practice Tags :
  • python

Similar Reads

  • How to show webcam in TkInter Window - Python
    Python offers various modules for creating GUI applications, out of which, Tkinter lets users do various tasks inside their app. Thus, while creating a GUI app, have you ever felt the need to let the user open the camera on a specific condition? Don't know, how to achieve that. Continue reading this
    5 min read
  • How To Center A Window On The Screen In Tkinter?
    Tkinter provides a basic geometry manager to control the placement of widgets within a window, it doesn't offer a built-in method to automatically center a window on the screen. However, achieving this task is relatively straightforward with a few lines of code. In this article, we'll explore differ
    2 min read
  • How to close only the TopLevel window in Python Tkinter?
    In this article, we will be discussing how to close only the TopLevel window in Python Tkinter. Syntax: def exit_btn():     top.destroy()     top.update() btn = Button(top,text='#Text of the exit button',command=exit_btn) Stepwise Implementation Step 1: First of all, import the library tkinter. from
    3 min read
  • PyQt5 – How to hide the title bar of window ?
    When we design the GUI (Graphical User Interface) application using PyQt5, there exist the window. A window is a (usually) rectangular portion of the display on a computer monitor that presents its contents (e.g., the contents of a directory, a text file or an image) seemingly independently of the r
    2 min read
  • How to Create Full Screen Window in Tkinter?
    Prerequisite: Tkinter There are two ways to create a full screen window in tkinter using standard python library for creating GUI applications. Method 1: Using attributes() function Syntax: window_name.attributes('-fullscreen',True) We will set the parameter '-fullscreen' of attributes() to True for
    3 min read
  • How to close a window in Tkinter?
    Python offers multiple options for developing GUI (Graphical User Interface). Out of all the GUI methods, tkinter is the most commonly used method. It is a standard Python interface to the Tk GUI toolkit shipped with Python. Python with tkinter is the fastest and easiest way to create GUI applicatio
    1 min read
  • How to use Thread in Tkinter Python
    Prerequisite:  Python GUI – tkintermultithreading Python offers multiple options for developing GUI (Graphical User Interface). Out of all the GUI methods, tkinter is the most commonly used method. It is a standard Python interface to the Tk GUI toolkit shipped with Python. Python with tkinter is th
    2 min read
  • Hide and Unhide The Window in Tkinter - Python
    Prerequisite: Tkinter Python offers multiple options for developing GUI (Graphical User Interface). Out of all the GUI methods, Tkinter is the most commonly used method. It is a standard Python interface to the Tk GUI toolkit shipped with Python. Python with Tkinter is the fastest and easiest way to
    2 min read
  • How to play sounds in Python with Tkinter?
    Python GUI tkinter is very useful when we want to take data from users. User attracts from GUI. GUI is very helpful in day to day life. A graphical user interface helps us to make our daily tasks easier and very productive. If you want to play music with the help of python GUI tkinter, then you come
    3 min read
  • How To Bind The Enter Key To A Tkinter Window?
    Tkinter is a Python library to develop GUI applications. Tkinter has many useful widgets that are necessary to build desktop applications. In this article, we will learn how to bind an Entry widget with a Tkinter Window. Binding Key in TkinterA key event occurs when the user clicks on any key on the
    3 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences