Tkinter는 창을 닫기 위한 사용자 정의 핸들러를 제공합니다. 사용자가 창을 닫기 위해 실행할 수 있는 콜백 함수 역할을 합니다.
핸들러를 사용하여 창을 닫으려면 destroy()를 사용할 수 있습니다. 방법. 함수나 위젯에서 호출한 후 창을 갑자기 닫습니다. 메서드를 정의하여 닫기 이벤트 핸들러를 호출해 보겠습니다.
위젯에서 인수로 사용
예
#Importing the required library
from tkinter import *
#Create an instance of tkinter frame or window
win= Tk()
#Set the geometry
win.geometry("600x400")
#Create a button and pass arguments in command as a function name
my_button= Button(win, text= "X", font=('Helvetica bold', 20),
borderwidth=2, command= win.destroy)
my_button.pack(pady=20)
win.mainloop() 함수를 호출하여
#Importing the required library
from tkinter import *
#Create an instance of tkinter frame or window
win= Tk()
#Set the geometry
win.geometry("600x300")
#Define a function
def close():
win.destroy()
#Create a button and pass arguments in command as a function name
my_button= Button(win, text= "X", font=('Helvetica bold', 20),
borderwidth=2, command= close)
my_button.pack(pady=20)
win.mainloop() 출력
위의 코드를 실행하면 "X" 버튼이 생성되고 그 위에 클릭하면 기본 창을 닫을 수 있습니다.
