필요할 때마다 위젯을 표시하고 숨길 수 있는 응용 프로그램을 만들어야 한다고 가정해 보겠습니다.
-
위젯은 pack_forget()을 통해 숨길 수 있습니다. 방법.
-
숨겨진 위젯을 표시하려면 pack()을 사용할 수 있습니다. 방법.
두 메서드 모두 람다 또는 익명 함수를 사용하여 호출할 수 있습니다.
예시
#Import the required library
from tkinter import *
#Create an instance of tkinter frame
win= Tk()
#Define the geometry of the window
win.geometry("650x450")
#Define function to hide the widget
def hide_widget(widget):
widget.pack_forget()
#Define a function to show the widget
def show_widget(widget):
widget.pack()
#Create an Label Widget
label= Label(win, text= "Showing the Message", font= ('Helvetica bold', 14))
label.pack(pady=20)
#Create a button Widget
button_hide= Button(win, text= "Hide", command= lambda:hide_widget(label))
button_hide.pack(pady=20)
button_show= Button(win, text= "Show", command= lambda:show_widget(label))
button_show.pack()
win.mainloop() 출력
위의 코드를 실행하면 위젯을 표시하거나 숨기는 데 사용할 수 있는 "표시"와 "숨기기" 두 개의 버튼이 있는 창이 표시됩니다.

이제 "숨기기" 버튼을 클릭하여 라벨 텍스트를 숨기고 "표시" 버튼을 클릭하여 라벨 텍스트를 표시합니다.
