Devdit
 

Python ปิดโปรแกรม tkinter ด้วยคำสั่ง destroy

1.2K

Python ปิดโปรแกรม tkinter ด้วยคำสั่ง destroy บทความนี้สอนใช้คำสั่ง destroy เพื่อปิดหน้าต่างโปรแกรมภาษา Python ที่พัฒนาด้วย tkinter module โดยใช้งานร่วมกับปุ่ม Button สามารถเขียนโปรแกรมได้ดังนี้

 

ตัวอย่าง Python ปิดโปรแกรม tkinter ด้วยคำสั่ง destroy

from tkinter import *   
from tkinter.messagebox import askyesno

def exit_program():
    app.destroy()

app = Tk()
app.geometry('400x200')
btn = Button( app, text='ปิดโปรแกรม', font=('Tahoma', 18), command=exit_program)
btn.pack(expand=True)
app.mainloop()

คำอธิบาย

Python ปิดโปรแกรม จากตัวอย่างเมื่อกดคลิกปุ่ม ‘ปิดโปรแกรม’ ภาษา Python จะเรียกใช้ฟังก์ชัน exit_program เพื่อปิดหน้าต่างโปรแกรมด้วยคำสั่ง app.destroy() ด้วย tkinter module และภาษา Python

แก้ไข 1 ปีที่แล้ว
ชอบ
ลิ้งก์
แชร์
Devdit มีช่อง YouTube แล้ว
เราสร้างวิดีโอเกี่ยวกับเทคโนโลยี ทำตามง่ายๆ