Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> Python

ฟังก์ชันปิดหน้าต่างใน Tkinter


ภาพรวม

เมื่อรันแอปพลิเคชัน Tkinter จะแสดงหน้าต่าง GUI ที่มี Widget, Frame และองค์ประกอบอื่นๆ สมมติต้องการปิดแอปด้วยฟังก์ชัน เมธอด destroy() ใน Python Tkinter ใช้ยุติการทำงานปกติหลังฟังก์ชัน mainloop

ตัวอย่างโค้ด

# Import the tkinter library
from tkinter import *
# Create an instance of tkinter frame
win = Tk()
# Set the geometry
win.geometry("650x250")
# Define a function
def close_app():
    win.destroy()
# Create a text Label
Label(win, text="Click to close the Window", font=('Helvetica bold', 14)).pack(pady=20)
# Create a Button for closing the window
button = Button(win, text="Close", command=close_app, font=('Helvetica bold', 10))
button.pack(pady=10)
win.mainloop()

ผลลัพธ์

รันโค้ดจะแสดงหน้าต่างที่มี Button ใช้ปิดหน้าต่าง

ฟังก์ชันปิดหน้าต่างใน Tkinter

คลิก "Close" เพื่อปิดหน้าต่าง