การสร้างแอปพลิเคชันโดยใช้ tkinter นั้นง่าย แต่บางครั้ง การปิดหน้าต่างหรือเฟรมโดยไม่ปิดผ่านปุ่มบนแถบชื่อทำได้ยาก ในกรณีเช่นนี้ เราสามารถใช้ .destroy() วิธีการปิดหน้าต่าง
เนื่องจากแอตทริบิวต์ tkinter เป็นอิสระจากกัน เราจึงสร้างวิธีการแยกเพื่อปิดหน้าต่างโดยใช้ปุ่มได้
ตัวอย่าง
#Import the library
from tkinter import *
#Create an instance of window
win = Tk()
#Set the geometry of the window
win.geometry("700x400")
#Define a function to close the window
def close_win():
win.destroy()
#Create a label
Label(win, text="Click the button to Close the window",
font=('Poppins bold', 25)).pack(pady= 20)
#Create a Button
Button(win, text= "Close", font=('Poppins bold', 16),
command=close_win).pack(pady=20)
win.mainloop() ผลลัพธ์
หากเราเรียกใช้โค้ดข้างต้น มันจะสร้างหน้าต่างผลลัพธ์ต่อไปนี้ -
