📜  如何隐藏、恢复和删除 Tkinter 小部件?

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

如何隐藏、恢复和删除 Tkinter 小部件?

Tkinter是一个用于创建 GUI(图形用户界面)应用程序的Python包。 Tkinter为我们提供了各种常见的 GUI 元素,我们可以用它们来构建界面——例如 按钮、标签、框架、消息、菜单、 以及各种输入字段和显示区域。我们将这些元素称为 Widgets。

小部件是图形用户界面 (GUI) 的一个元素,用于说明信息,以便用户可以与操作系统交互。在 Tkinter 中,Widgets 是对象;表示按钮、框架等的类的实例。

在本文中,我们将演示如何使用按钮、标签、框架等小部件的各种元素来隐藏、恢复和删除 Tkinter 小部件。

我们可以通过调用 pack_forget() 方法隐藏 Tkinter 小部件以使小部件不可见。我们需要再次调用 pack() 方法来打包小部件以使其可见,或者恢复它。我们还可以通过调用本节中的 destroy() 方法永久删除 Tkinter 小部件。

示例 1:使用Forget_pack( )pack()方法在 Tkinter 中隐藏和恢复按钮。

句法:

widget.forget_pack()

小部件可以是任何可见的有效小部件。

Python3
# Imports tkinter
from tkinter import *
  
# toplevel window
root = Tk()
  
# Method to make Button(Widget) invisible from toplevel
  
  
def hide_button(widget):
    # This will remove the widget from toplevel
    widget.pack_forget()
  
  
# Method to make Button(widget) visible
def show_button(widget):
    # This will recover the widget from toplevel
    widget.pack()
  
  
# Button widgets
B1 = Button(root, text="Button 1")
B1.pack()
  
  
# See, in command hide_button() function is passed to hide Button B1
B2 = Button(root, text="Button 2", command=lambda: hide_button(B1))
B2.pack()
  
# In command show_button() function is passed to recover Button B1
B3 = Button(root, text="Button 3", command=lambda: show_button(B1))
B3.pack()
  
root.mainloop()


Python3
# Imports tkinter
from tkinter import *
  
# toplevel window
root = Tk()
  
# label widget
label = Label(root, text="LABEL")
  
  
# Method to make Label(Widget) invisible
def hide_label():
    # This will remove the widget
    label.pack_forget()
  
  
# Method to make Label(widget) visible
def recover_label():
    # This will recover the widget
    label.pack()
  
  
# hide_label() function hide the label temporarily
B2 = Button(root, text="Click To Hide label", fg="red", command=hide_label)
B2.pack()
  
# recover_label() function recover the label
B1 = Button(root, text="Click To Show label",
            fg="green", command=recover_label)
B1.pack()
  
# pack Label widget
label.pack()
  
# Start the GUI
root.mainloop()


Python3
from tkinter import *
  
# toplevel window
root = Tk()
  
# label widget
mylabel = Label(root, text="GeeksforGeeks")
mylabel.pack()
  
# delete_label() function  to delete Label(Widget) permanently
  
  
def delete_label():
    mylabel.destroy()
  
  
# Creating button. In this destroy method is passed
# as command, so as soon as button is pressed
# Label will be destroyed
B1 = Button(root, text="DELETE", command=delete_label)
B1.pack()
  
# start the GUI
root.mainloop()


输出:

示例 2:使用 Forgot_pack() 和 pack() 方法在 Tkinter 中隐藏和恢复标签。  

蟒蛇3

# Imports tkinter
from tkinter import *
  
# toplevel window
root = Tk()
  
# label widget
label = Label(root, text="LABEL")
  
  
# Method to make Label(Widget) invisible
def hide_label():
    # This will remove the widget
    label.pack_forget()
  
  
# Method to make Label(widget) visible
def recover_label():
    # This will recover the widget
    label.pack()
  
  
# hide_label() function hide the label temporarily
B2 = Button(root, text="Click To Hide label", fg="red", command=hide_label)
B2.pack()
  
# recover_label() function recover the label
B1 = Button(root, text="Click To Show label",
            fg="green", command=recover_label)
B1.pack()
  
# pack Label widget
label.pack()
  
# Start the GUI
root.mainloop()

输出:

示例 3:通过调用destroy()方法永久删除 Tkinter 小部件。我们可以将此方法用于任何可用的小部件以及主 tkinter 窗口。

蟒蛇3

from tkinter import *
  
# toplevel window
root = Tk()
  
# label widget
mylabel = Label(root, text="GeeksforGeeks")
mylabel.pack()
  
# delete_label() function  to delete Label(Widget) permanently
  
  
def delete_label():
    mylabel.destroy()
  
  
# Creating button. In this destroy method is passed
# as command, so as soon as button is pressed
# Label will be destroyed
B1 = Button(root, text="DELETE", command=delete_label)
B1.pack()
  
# start the GUI
root.mainloop()

输出: