如何使 Tkinter 组件不可见?
若要使 tkinter 组件不可见,我们可以使用 pack_forget() 方法。它通常用于取消映射窗口中的组件。
示例
在下例中,我们将创建一个文本标签和一个按钮,该按钮可用于触发文本标签组件上的不可见事件。
#Import the required libraries
from tkinter import *
#Create an instance of tkinter frame
win= Tk()
#Set the geometry of frame
win.geometry("600x250")
#Set the resizable property False
win.resizable(False, False)
#Make the widgets Invisible
def make_invisible(widget):
widget.pack_forget()
#Create a label for the window or frame
label=Label(win, text="Hello World!", font=('Helvetica bold',20),
anchor="center")
label.pack(pady=20)
#Create a button to make the widgets invisible
btn=Button(win, text="Click", font= ('Helvetica bold', 10), command=lambda:
make_invisible(label))
btn.pack(pady=20)
win.mainloop()输出
运行以上代码会生成以下窗口 −

现在单击“单击”按钮,使文本标签不可见。

广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP