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

วิธีปิดใช้งานปุ่มปิดหน้าต่าง (X) ใน Tkinter

Window Manager ควบคุมไอคอนควบคุมหน้าต่าง (Minimize, Maximize, Close) สามารถปิดใช้งานปุ่มปิด (X) ได้โดยกำหนดโปรโตคอล WM_DELETE_WINDOW ให้ทำงานเป็นฟังก์ชันว่าง

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

# Import the tkinter library
from tkinter import *
from tkinter import ttk

# Create an instance of Tkinter frame
win = Tk()

# Define the geometry of the function
win.geometry("750x250")

def close_win():
    win.destroy()

def disable_event():
    pass

# Create a button to close the window
btn = ttk.Button(win, text="Click here to Close", command=close_win)
btn.pack()

# Disable the Close Window Control Icon
win.protocol("WM_DELETE_WINDOW", disable_event)

win.mainloop()

ผลลัพธ์

หน้าต่างจะมีปุ่ม X ที่กดไม่ได้ ต้องกดปุ่ม "Click here to Close" เพื่อปิด

วิธีปิดใช้งานปุ่มปิดหน้าต่าง (X) ใน Tkinter