Tkinter จัดเตรียมตัวจัดการแบบกำหนดเองเพื่อปิดหน้าต่าง ทำหน้าที่เป็นฟังก์ชันเรียกกลับที่ผู้ใช้สามารถเรียกใช้เพื่อปิดหน้าต่างได้
ในการปิดหน้าต่างโดยใช้ตัวจัดการ เราสามารถใช้ ทำลาย() กระบวนการ. มันปิดหน้าต่างกะทันหันหลังจากเรียกมันในฟังก์ชั่นหรือวิดเจ็ตใด ๆ ให้เราเรียกใช้ตัวจัดการเหตุการณ์ปิดโดยกำหนดวิธีการ
โดยใช้เป็นอาร์กิวเมนต์ใน Widget
ตัวอย่าง
#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" และเมื่อคลิกเข้าไป เราจะปิดหน้าต่างหลักได้
