-2

tkinter关闭窗口后,我的程序必须执行一些任务。

当按下'X'按钮时,它应该打印“窗口已关闭,怎么办”???

我的代码是

from tkinter import *
root = Tk()
Label(root, text = "This is for stackoverflow('X' button clicked code)").pack()
root.mainloop()

其实我正在寻找类似返回值的东西......

4

2 回答 2

1

您可以使用WM_DELETE_WINDOW协议来检测根窗口是否关闭。

root.protocol("WM_DELETE_WINDOW", lambda: print ("The window is closed."))

此外,mainloop()它是一种保持 GUI 运行的 while 循环。

也可以print('The window is closed.')在后面加上

root.mainloop()
print('The window is closed.')
于 2021-07-24T06:37:00.037 回答
0

尝试在结束后打印语句mainloop()。看看@BryanOakley 的评论——

root.mainloop()
print("The window is closed")

在关闭窗口或主循环结束之前,打印语句不会运行

编辑-也许这样的事情会起作用-

import tkinter as tk

root = tk.Tk()

l = tk.Label(root,text='hiiii')
l.pack()

root.mainloop()

print('The application is closed, new window opening')


win = tk.Tk()

l = tk.Label(win,text='byeee')
l.pack()

win.mainloop()
于 2021-07-24T06:42:10.840 回答