หากต้องการออกจาก Python โดยใช้ปุ่ม Tkinter คุณสามารถทำตามขั้นตอนด้านล่าง -
ขั้นตอน -
-
นำเข้าไลบรารี tkinter และสร้างอินสแตนซ์ของเฟรม tkinter
-
กำหนดขนาดของเฟรมโดยใช้เรขาคณิต วิธีการ
-
กำหนดฟังก์ชัน close() เพื่อปิดหน้าต่าง เรียกเมธอด win.destroy() ภายใน ปิด() .
-
ต่อไป สร้างปุ่มและเรียก close() ฟังก์ชัน
-
สุดท้าย เรียกใช้ mainloop ของหน้าต่างแอปพลิเคชัน
ตัวอย่าง
# 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() ออกจากแอปพลิเคชันกะทันหันซึ่งหมายความว่า mainloop จะยังคงทำงานในพื้นหลัง win.destroy() ในอีกทางหนึ่งยุติ mainloop และทำลายวิดเจ็ตทั้งหมดภายในหน้าต่าง