I checked some posts on Stack Overflow on how to override the window closing event and add some logic there. But the app.protocol("WM_DELETE_WINDOW", on_close
code makes sure the function attached is executed when the same protocol line is executed. In simple terms the code in the on_close function gets executed immediately the interpreter see the line above. You can see from the screenshot below that the method printed some words and then showed the window.
我查看了一些关于如何覆盖窗口关闭事件并在其中添加一些逻辑的Stack Overflow帖子。但app.protocol(“WM_DELETE_WINDOW”,ON_CLOSE代码)确保在执行相同的协议行时执行附加的函数。简而言之,ON_CLOSE函数中的代码立即执行,解释器会看到上面的代码行。从下面的屏幕截图中可以看到,该方法打印了一些单词,然后显示了窗口。
The line exiting the application has already been printed to the console when it's supposed to be printed when the application is dying as a result of the user clicking the close button(alias the X icon on the title bar).
This is the code am using
当应用程序由于用户单击关闭按钮(标题栏上的X图标别名)而终止时,退出应用程序的行已经打印到控制台。这是我使用的代码
import tkinter as tk
# function to call when user clicks close
def close_code():
print("exiting the application")
def build_gui():
#create a new window
window = tk.Tk()
window.title("Chat App")
window.geometry("500x500")
window.resizable(width = False, height= False)
window.configure(bg='white')
#add the protocol, this line calls close code instead of waiting for user to close the app
window.protocol("WM_DELETE_WINDOW", close_code)
window.mainloop()
build_gui()
How do I accurately achieve this without getting the method called when the window is being built ?
如何在不调用生成窗口时调用的方法的情况下准确地实现这一点?
更多回答
Please correct the error in the code. close_code()
most likely instead of close_code
.
请更正代码中的错误。close_code()而不是close_code。
@СергейКох, question edited
@ Zuerzuerzuerzuerzuerzuerzuerzuerzuerzuerzuerzuerzuerzuerzuerzuerzuerzuerzuerzuerzuerzuerzuerzuerzuerzuerzuerzuerzuerzuerzuerzuerzuerzuerzuerzuerzuerzuerzuerzuerzuerzuerzuerzuerzuerzuerzuerzuerzuerzuerzuerzuerzuerzuerzuerzuerzuerzuerzuerzuerzuerzuerzuerzuerzuerzuerzuerzuerzuerzuerzu
Then your error will not be reproduced.
那么你的错误将不会被重现。
Is this related to your issue ?
这和你的问题有关吗?
@СергейКох, writing close_code() would call the function immediately. We want to just pass the function name.
@СергейКох,编写CLOSE_CODE()将立即调用该函数。我们只想传递函数名。
优秀答案推荐
Not sure if this code is what you want to do :
不确定此代码是否为您想要执行的操作:
import tkinter as tk
def close_code(window): #pass your window as param
print("exiting the application")
window.destroy() #destroy your window
def build_gui():
#create a new window
window = tk.Tk()
window.title("Chat App")
window.geometry("500x500")
window.resizable(width = False, height= False)
window.configure(bg='white')
#add the protocol, this line calls close code instead of waiting for user to close the app
window.protocol("WM_DELETE_WINDOW", lambda window=window : close_code(window)) #lambda param1=SOMETHING : fct(param1)
window.mainloop()
build_gui()
I recommand you to use object oriented when using Tkinter :)
Have a nice day !
我建议您在使用Tkinter时使用面向对象:)祝您愉快!
更多回答
我是一名优秀的程序员,十分优秀!