在Python中使用 Tkinter 的 Place_forget() 方法
要在 tkinter 中隐藏或忘记父窗口小部件或屏幕中的窗口小部件, place_forget ( ) 方法将根据位置几何管理在该窗口小部件上使用。
Syntax: widget.place_forget()
Parameter: None
Return: None
下面是实现:
Python3
# Imports everything from tkinter
# and ttk module
from tkinter import *
from tkinter.ttk import *
# toplevel window
root = Tk()
# setting window size
root.geometry("150x100")
# label widget
label = Label(root, text="LABEL")
# place in the window
label.place(relx=0.4, y=5)
# button widgets
# In command attribute of Button,
# place_forget() method is passed
# in the lambda function to temporarily
# hide the label
b1 = Button(root, text = "hide text",
command = lambda: label.place_forget())
b1.place(relx = 0.3, y = 30)
# the label is placed again
b2 = Button(root, text = "retrieve text",
command = lambda: label.place(
relx = 0.4))
b2.place(relx = 0.3, y = 50)
# Start the GUI
root.mainloop()
输出:
隐藏后:
取回后:
注意:还有其他方法pack_forget()和grid_forget()的工作方式与 forget_pack() 和 forget_grid() 相同。