Tkinter 버튼을 사용하여 Python을 종료하려면 다음 단계를 따르세요. -
단계 -
-
tkinter 라이브러리를 가져오고 tkinter 프레임의 인스턴스를 만듭니다.
-
기하학을 사용하여 프레임 크기 설정 방법.
-
close() 함수를 정의하여 창을 닫습니다. win.destroy() 메서드를 호출합니다. close() 내부 .
-
다음으로 버튼을 만들고 close()를 호출합니다. 기능.
-
마지막으로 메인 루프를 실행합니다. 응용 프로그램 창의.
예시
# Import the library
from tkinter import *
# Create an instance of window
win = Tk()
# Set the geometry of the window
win.geometry("700x350")
# Title of the window
win.title("Click the Button to Close the Window")
# Define a function to close the window
def close():
#win.destroy()
win.quit()
# Create a Button to call close()
Button(win, text= "Close the Window", font=("Calibri",14,"bold"), command=close).pack(pady=20)
win.mainloop() 출력
실행 시 다음과 같은 출력을 생성합니다 -

버튼을 클릭하면 창이 닫힙니다.
win.destroy() 대신 , win.quit()를 사용할 수도 있습니다. 응용 프로그램을 닫습니다. 그러나 둘 사이에는 미묘한 차이가 있습니다. win.quit() 메인 루프가 백그라운드에서 계속 실행되고 있음을 의미하는 애플리케이션을 갑자기 종료합니다. win.destroy() 반면에 메인루프를 종료합니다. 창 안의 모든 위젯을 파괴합니다.