Python | winfo_ismapped() and winfo_exist() in Tkinter

Tkinter provides numerous of universal widget methods or basic widget methods which works almost with all the available widgets.

winfo_ismapped() method –

This method is used to check whether the specified widget is visible or not.

Syntax: widget.winfo_ismapped()
Return Value: Returns True if widget is visible (or mapped), otherwise returns False.
Exception: If widget is destroyed, then it throws error.

filter_none

edit
close

play_arrow

link
brightness_4
code

# Imports tkinter and ttk module
from tkinter import * 
from tkinter.ttk import * 
import time
  
# toplevel window
root = Tk()
  
def forget(widget):
    widget.forget()
    print("After Forget method called. Is widget mapped? = ",
                               bool(widget.winfo_ismapped()))
  
def retrieve(widget):
    widget.pack()
    print("After retrieval of widget. Is widget mapped? = ",
                                bool(widget.winfo_exists()))
  
# Button widgets
b1 = Button(root, text = "Btn 1")
b1.pack()
  
# This is used to make widget invisible
b2 = Button(root, text = "Btn 2", command = lambda : forget(b1))
b2.pack()
   
# This will retrieve widget
b3 = Button(root, text = "Btn 3", command = lambda : retrieve(b1))
b3.pack()
  
# infinite loop, interrupted by keyboard or mouse
mainloop()

chevron_right


Output:

winfo_exists() method –

This method is used to check if the specified widget exists or not i.e if the widget is destroyed or not.

Syntax: widget.winfo_exists()
Retrun value: Returns True if widget exists, False otherwise.

filter_none

edit
close

play_arrow

link
brightness_4
code

# Imports tkinter and ttk module
from tkinter import * 
from tkinter.ttk import *
  
# toplevel window
root = Tk()
  
def dest(widget):
    widget.destroy()
    print("Destroy method called. Widget exists? = ",
                          bool(widget.winfo_exists()))
      
def exist(widget):
    print("Checking for existance = ", bool(widget.winfo_exists()))
  
# Button widgets
b1 = Button(root, text = "Btn 1")
b1.pack()
  
# This is used to destroy widget
b2 = Button(root, text = "Btn 2", command = lambda : dest(b1))
b2.pack()
   
# This is used to check existance of the widget
b3 = Button(root, text = "Btn 3", command = lambda : exist(b1))
b3.pack()
  
# infinite loop, interrupted by keyboard or mouse
mainloop()

chevron_right


Output:

Note: If a widget is destroyed it cannot be retrieved again.



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.