Learning python help

Hi there

I am learning python right now, and am making a simple console game. I just need help with one thing. Is there a way to make python do something when you clck the X or close it in any way? Like store a value in a text file.


if __name__ == "__main__":
		with open("/tmp/exit_message", "w") as f:
			f.write("bye bye!")


2 things.

  1. Will that work if someone clicks the X?
    and 2. I’m still learning, so I have no idea how to use that.