Computer >> 컴퓨터 >  >> 프로그램 작성 >> Python

창에서 Tkinter 위젯을 삭제하는 방법은 무엇입니까?

<시간/>

때로는 응용 프로그램에서 사용하지 않는 위젯을 제거하고 싶을 때가 있습니다. .destroy를 사용하여 창이나 프레임에서 위젯을 삭제할 수 있습니다. tkinter의 메소드. 위젯에 대한 함수를 정의하여 위젯에서 호출할 수 있습니다.

예시

이 예에서는 창에서 텍스트 레이블 위젯을 제거하는 버튼을 만들었습니다.

#Import the tkinter library
from tkinter import *

#Create an instance of tkinter frame
win = Tk()

#Set the geometry
win.geometry("650x450")

#Define a function to remove the text from the screen
def delete_text():
   text.destroy()

#Create a text widget
text= Label(win,text="This is a New Line", font=('Aerial bold', 20))
text.pack(pady=20)

#Create a button for Deleting Widget
Button(win, text= "Click Here", font=('bold',20), command=
delete_text).pack(pady=10)

win.mainloop()

출력

위의 코드를 실행하면 다음과 같은 출력이 생성됩니다 -

창에서 Tkinter 위젯을 삭제하는 방법은 무엇입니까?

이제 "여기를 클릭하십시오" 버튼을 클릭하십시오. 창에서 레이블 텍스트 위젯을 삭제합니다.

창에서 Tkinter 위젯을 삭제하는 방법은 무엇입니까?