Tkinter 애플리케이션에서 프레임(Frame)은 매우 유용한 구성 요소입니다. 애플리케이션에서 프레임을 정의하면 그 안에 여러 위젯을 하나의 그룹으로 묶어 배치할 수 있습니다. 이때 프레임 내부에 포함된 위젯들을 해당 프레임의 자식(children)이라고 부릅니다.
그렇다면 프레임에 정의된 모든 자식 위젯을 한 번에 제거하려면 어떻게 해야 할까요? 먼저 winfo_children() 메서드를 사용하여 프레임의 자식 위젯 목록을 가져옵니다. 그런 다음 각 자식 위젯에 대해 destroy() 메서드를 호출하면 기존의 모든 자식 위젯을 손쉽게 삭제할 수 있습니다.
예제 코드
#Import the Tkinter Library
from tkinter import *
#Create an instance of Tkinter Frame
win = Tk()
#Set the geometry of window
win.geometry("700x350")
#Initialize a Frame
frame = Frame(win)
def clear_all():
for item in frame.winfo_children():
item.destroy()
button.config(state= "disabled")
#Define a ListBox widget
listbox = Listbox(frame, height=10, width= 15, bg= 'grey', activestyle= 'dotbox',font='aerial')
listbox.insert(1,"Go")
listbox.insert(1,"Java")
listbox.insert(1,"Python")
listbox.insert(1,"C++")
listbox.insert(1,"Ruby")
listbox.pack()
label = Label(win, text= "Top 5 Programming Languages", font= ('Helvetica 15 bold'))
label.pack(pady= 20)
frame.pack()
#Create a button to remove all the children in the frame
button = Button(win, text= "Clear All", font= ('Helvetica 11'), command= clear_all)
button.pack()
win.mainloop()실행 결과
위 코드를 실행하면 리스트박스(Listbox)에 항목들이 표시된 창과 함께 버튼이 나타납니다.

이 상태에서 "Clear All" 버튼을 클릭하면 프레임 객체 내부에 있는 모든 자식 위젯이 화면에서 제거됩니다.

핵심 정리
정리하면 다음 두 가지 메서드만 기억하면 됩니다.
- winfo_children(): 특정 위젯(프레임)의 모든 자식 위젯 목록을 반환합니다.
- destroy(): 해당 위젯을 완전히 제거하여 화면에서 사라지게 합니다.
이 두 메서드를 조합하면 동적으로 생성한 위젯들을 일괄 초기화해야 하는 상황(예: 목록 새로고침, 화면 전환 등)에서 매우 편리하게 활용할 수 있습니다.