Create a Python GUI program to close a window
Python tkinter Basic: Exercise-18 with Solution
Write a Python GUI program to create a window that closes when a "Close" button is clicked.
import tkinter as tk # Function to close the window def close_window(): parent.destroy() # Create the main window parent = tk.Tk() parent.title("Close Window Example") # Create a label label = tk.Label(parent, text="Click the 'Close' button to close this window.") label.pack(padx=25, pady=25) # Create a close button close_button = tk.Button(parent, text="Close", command=close_window) close_button.pack() # Start the Tkinter event loop parent.mainloop()
In the exercise above -
- First we import the 'tkinter' library and create the main window using tk.Tk().
- Next we define a function called "close_window()" that calls root.destroy() to close the main window.
- A label and a "Close" button are added to the window. The button's command parameter is set to 'close_window', so clicking the button will execute the "close_window()" function.
- The program starts the Tkinter event loop using parent.mainloop(), which keeps the GUI window running.
Python Code Editor:
Previous: Create interactive tooltips in a Python Tkinter window.
Next: Create a Python Tkinter application with color picker.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.
Python: Tips of the Day
Summing a sequence of numbers (calculating the sum of zero to ten with skips):
>>> l = range(0,10,2) >>> sum(l) 20
- Weekly Trends
- Python Interview Questions and Answers: Comprehensive Guide
- Scala Exercises, Practice, Solution
- Kotlin Exercises practice with solution
- MongoDB Exercises, Practice, Solution
- SQL Exercises, Practice, Solution - JOINS
- Java Basic Programming Exercises
- SQL Subqueries
- Adventureworks Database Exercises
- C# Sharp Basic Exercises
- SQL COUNT() with distinct
- Java Collection Exercises
- SQL COUNT() function
- SQL Inner Join
We are closing our Disqus commenting system for some maintenanace issues. You may write to us at reach[at]yahoo[dot]com or visit us at Facebook