📜  Python| Tkinter 中的 winfo_ismapped() 和 winfo_exists()

📅  最后修改于: 2022-05-13 01:55:47.534000             🧑  作者: Mango

Python| Tkinter 中的 winfo_ismapped() 和 winfo_exists()

Tkinter 提供了许多通用的小部件方法或基本的小部件方法,它们几乎适用于所有可用的小部件。

winfo_ismapped() 方法 –

此方法用于检查指定的小部件是否可见。

Puython
# 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()


Python
# 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 existence = ", 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 existence of the widget
b3 = Button(root, text = "Btn 3", command = lambda : exist(b1))
b3.pack()
  
# infinite loop, interrupted by keyboard or mouse
mainloop()


输出:

Tkinter 1 中的 winfo_ismapped()

Tkinter 2 中的 winfo_ismapped()

winfo_exists() 方法 –

此方法用于检查指定的小部件是否存在,即小部件是否被销毁。

Python

# 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 existence = ", 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 existence of the widget
b3 = Button(root, text = "Btn 3", command = lambda : exist(b1))
b3.pack()
  
# infinite loop, interrupted by keyboard or mouse
mainloop()

输出:

tkinter 1 中的 winfo_ismapped() 方法

tkinter 2 中的 winfo_ismapped() 方法

tkinter 中的 winfo_ismapped() 方法

注意:如果小部件被销毁,则无法再次检索它。