tkinter window 关闭时如何执行最后一个函数
How to execute a last function when tkinter window gets closed
我正在设计一个程序,我希望它在根 window 被用户关闭时自动保存。我已经创建了该功能,但它不是必需的。如何将函数绑定到 window 关闭事件?
示例代码:
import sys
import tkinter as tk
import tkinter.ttk as ttk
from tkinter.constants import *
def main(*args):
global root
root = tk.Tk()
root.protocol('WM_DELETE_WINDOW', root.destroy)
global _top2, _w2
_top2 = root
_w2 = spiel(_top2)
root.mainloop()
class spiel:
def __init__(self, top=None):
_bgcolor = '#d9d9d9' # X11 color: 'gray85'
_fgcolor = '#000000' # X11 color: 'black'
_compcolor = '#d9d9d9' # X11 color: 'gray85'
_ana1color = '#d9d9d9' # X11 color: 'gray85'
_ana2color = '#ececec' # Closest X11 color: 'gray92'
self.style = ttk.Style()
if sys.platform == "win32":
self.style.theme_use('winnative')
self.style.configure('.',background=_bgcolor)
self.style.configure('.',foreground=_fgcolor)
self.style.map('.',background=
[('selected', _compcolor), ('active',_ana2color)])
top.geometry("600x450+475+21")
top.minsize(600, 450)
top.maxsize(600, 450)
top.resizable(0,0)
top.title("mathe")
top.configure(background="#d9d9d9")
top.configure(highlightbackground="#d9d9d9")
top.configure(highlightcolor="black")
self.top = top
self.Frame1 = tk.Frame(self.top)
self.Frame1.place(relx=0.017, rely=0.133, relheight=0.856
, relwidth=0.975)
def function():
print(fr"This function shall be exectuted when the user closes the window.")
if __name__ == '__main__':
main()
信息:
我不想阻止关闭按钮并使用 root.quit() 创建我自己的版本,我希望用户拥有正常的关闭按钮。
如果你知道怎么做,请告诉我,谢谢:)
您应该在 root.protocol('WM_DELETE_WINDOW', ...)
中使用 function
而不是 root.destroy
并在 function()
末尾调用 root.destroy()
:
...
def function():
print(fr"This function shall be exectuted when the user closes the window.")
root.destroy()
def main(*args):
global root
root = tk.Tk()
root.protocol('WM_DELETE_WINDOW', function) # call function() when window is closed
global _top2, _w2
_top2 = root
_w2 = spiel(_top2)
root.mainloop()
...
我正在设计一个程序,我希望它在根 window 被用户关闭时自动保存。我已经创建了该功能,但它不是必需的。如何将函数绑定到 window 关闭事件?
示例代码:
import sys
import tkinter as tk
import tkinter.ttk as ttk
from tkinter.constants import *
def main(*args):
global root
root = tk.Tk()
root.protocol('WM_DELETE_WINDOW', root.destroy)
global _top2, _w2
_top2 = root
_w2 = spiel(_top2)
root.mainloop()
class spiel:
def __init__(self, top=None):
_bgcolor = '#d9d9d9' # X11 color: 'gray85'
_fgcolor = '#000000' # X11 color: 'black'
_compcolor = '#d9d9d9' # X11 color: 'gray85'
_ana1color = '#d9d9d9' # X11 color: 'gray85'
_ana2color = '#ececec' # Closest X11 color: 'gray92'
self.style = ttk.Style()
if sys.platform == "win32":
self.style.theme_use('winnative')
self.style.configure('.',background=_bgcolor)
self.style.configure('.',foreground=_fgcolor)
self.style.map('.',background=
[('selected', _compcolor), ('active',_ana2color)])
top.geometry("600x450+475+21")
top.minsize(600, 450)
top.maxsize(600, 450)
top.resizable(0,0)
top.title("mathe")
top.configure(background="#d9d9d9")
top.configure(highlightbackground="#d9d9d9")
top.configure(highlightcolor="black")
self.top = top
self.Frame1 = tk.Frame(self.top)
self.Frame1.place(relx=0.017, rely=0.133, relheight=0.856
, relwidth=0.975)
def function():
print(fr"This function shall be exectuted when the user closes the window.")
if __name__ == '__main__':
main()
信息: 我不想阻止关闭按钮并使用 root.quit() 创建我自己的版本,我希望用户拥有正常的关闭按钮。
如果你知道怎么做,请告诉我,谢谢:)
您应该在 root.protocol('WM_DELETE_WINDOW', ...)
中使用 function
而不是 root.destroy
并在 function()
末尾调用 root.destroy()
:
...
def function():
print(fr"This function shall be exectuted when the user closes the window.")
root.destroy()
def main(*args):
global root
root = tk.Tk()
root.protocol('WM_DELETE_WINDOW', function) # call function() when window is closed
global _top2, _w2
_top2 = root
_w2 = spiel(_top2)
root.mainloop()
...