个性化阅读
专注于IT技术分析

Python Tkinter中的winfo_ismapped()和winfo_exist()

Tkinter提供了许多通用的窗口小部件方法或基本的窗口小部件方法, 它们几乎可以与所有可用的窗口小部件一起使用。

winfo_ismapped()方法–

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

语法:widget.winfo_ismapped()返回值:如果窗口小部件可见(或映射), 则返回True, 否则返回False。例外:如果小部件被破坏, 则将引发错误。

# 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 | Tkinter中的winfo_ismapped()和winfo_exist()1
Python | Tkinter中的winfo_ismapped()和winfo_exist()2

winfo_exists()方法–

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

语法:widget.winfo_exists()返回值:如果存在widget, 则返回True, 否则返回False。

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

输出如下:

Python | Tkinter中的winfo_ismapped()和winfo_exist()3
Python | Tkinter中的winfo_ismapped()和winfo_exist()4
Python | Tkinter中的winfo_ismapped()和winfo_exist()5

注意:如果小部件被破坏, 则无法再次检索。

首先, 你的面试准备可通过以下方式增强你的数据结构概念:Python DS课程。


赞(0)
未经允许不得转载:srcmini » Python Tkinter中的winfo_ismapped()和winfo_exist()

评论 抢沙发

评论前必须登录!