Tkinter 关闭行为
Tkinter 关闭行为
魔力刘易斯Tkinter 窗口的关闭行为可以通过 protocol 方法设置
代码实现示例
import tkinter as tk
import tkinter.messagebox as messagebox
import ctypes # 导入模块
root = tk.Tk()
ctypes.windll.shcore.SetProcessDpiAwareness(1)
ScaleFactor = ctypes.windll.shcore.GetScaleFactorForDevice(0)
root.tk.call("tk", "scaling", ScaleFactor / 75)
screen_width = root.winfo_screenwidth()
screen_height = root.winfo_screenheight()
window_width = 650
window_height = 190
x = int((screen_width - window_width) / 2)
y = int((screen_height - window_height) / 2)
root.geometry(f"{window_width}x{window_height}+{x}+{y}")
root.resizable(False, False)
def on_closing(): # 关闭窗口时触发
if messagebox.askokcancel("退出程序", "你确定要退出吗?"):
root.destroy()
root.protocol("WM_DELETE_WINDOW", on_closing) # 关闭窗口时触发
root.mainloop()