Computer >> 컴퓨터 >  >> 프로그램 작성 >> C++

Tkinter의 destroy() 메서드 - Python

<시간/>

Tkinter의 destroy() 메소드는 위젯을 파괴합니다. 서로 의존하는 다양한 위젯의 동작을 제어하는 ​​데 유용합니다. 또한 일부 사용자 작업으로 프로세스가 완료되면 GUI 구성 요소를 제거하여 메모리를 해제하고 화면을 지울 필요가 있습니다. destroy() 메소드는 이 모든 것을 달성합니다.

아래 예에는 3개의 버튼이 있는 화면이 있습니다. 첫 번째 버튼을 클릭하면 창 자체가 닫히고 두 번째 버튼을 클릭하면 첫 번째 버튼이 닫히는 식입니다. 이 동작은 아래 프로그램과 같이 destroy 메소드를 사용하여 에뮬레이트됩니다.

예시

from tkinter import *
from tkinter.ttk import *
#tkinter window
base = Tk()

#This button can close the window
button_1 = Button(base, text ="I close the Window", command = base.destroy)
#Exteral paddign for the buttons
button_1.pack(pady = 40)

#This button closes the first button
button_2 = Button(base, text ="I close the first button", command = button_1.destroy)
button_2.pack(pady = 40)

#This button closes the second button
button_3 = Button(base, text ="I close the second button", command = button_2.destroy)
button_3.pack(pady = 40)
mainloop()

위의 코드를 실행하면 다음과 같은 결과가 나옵니다. -

다양한 버튼을 클릭하면 프로그램에서 언급한 다양한 동작을 관찰할 수 있습니다.

Tkinter의 destroy() 메서드 - Python