반응형

GUI에서 객체를 생성했다면 객체를 파괴하는 방법에 대해서도 생각해볼 필요가 있다.


destroy라는 메서드를 이용하면 지울 수 있게되는데 예시를 통해 바로 알아보자.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
from Tkinter import *
 
window = Tk()
 
def clear():
    mylist = window.grid_slaves()
    for i in mylist:
        i.destroy()
 
def clearBot():
    mylist = bottomWindow.grid_slaves()
    for i in mylist:
        i.destroy()
        
Label(window,text='Hello World!').grid(row=0)
Button(window,text='Clear',command=clear).grid(row=1)
 
 
bottomWindow = Frame(window)
bottomWindow.grid(row = 2)
 
Label(bottomWindow, text='bottom Label').grid(row = 5)
Button(bottomWindow, text='bottom Button', command = clearBot).grid(row = 6)
 
window.mainloop()
 
 
//                                                       This source code Copyright belongs to Crocus
//                                                        If you want to see more? click here >>
cs



위의 코드는 우선 window라는 루트 객체를 만들고 그다음 라벨과 버튼을 붙여준다.


그 뒤에는 bottomWindow라는 프레임 객체를 만들어주고 window내에 프레임으로 존재하도록 해주고 라벨과 버튼을 bottomWindow에 붙여준다.


이제 위의 그림에서 Clear을 누르면 무슨 현상이 일어날까?


def clear을 보면 window.grid_slaves()라고 있다.


저 말은 window 객체내의 grid로 배치된 애들 모두 mylist에 담으라는 의미가 된다.


따라서 for문에 의해 destroy를 하면 모든 라벨, 버튼, 프레임을 파괴할 수 있다.


반면 아래 bottom Button을 누르면 Frame속의 객체만 삭제가 되는것을 확인 할 수 있다.(이때 bottomFrame은 파괴되지 않는다.)













반응형