I am making a python program which takes a lot's of user input. And If user by mistake close the program all the data stored in variables are gone. So, I want to make something like warning message like "Do you want to close the program" appear when ever user click on close button or do shortcut like alt+F4 to close program. I just want it for making my program much better. Cuz, If some one had input many data and he by mistake close the program and all the progress for him and my program as lost. So to overcome this I want to to know to make something like warning message appear whenever user try to close the program.
-
Which module are you using to make the program – Ibrahim Jan 21 '21 at 11:53
-
The Module are basics. Like time and os. It's not a great program. You can say that I am just making a attendance program. So If the user close the program all the data of roll numbers or something like that are gone for that session – Harish Kumawat Jan 21 '21 at 11:54
-
your program runs in terminal or in a GUI? – Ibrahim Jan 21 '21 at 11:58
-
my program run's in terminal – Harish Kumawat Jan 21 '21 at 12:04
-
then what's the problem if your program runs in terminal, IDE's like IDLE and PyCharm ask you if you click the cross-button or press alt-F4 'the process is still running do you want to close it' – Ibrahim Jan 21 '21 at 12:11
-
Pop ups are more for a gui program, you could just have a line asking if the user is sure come up on the terminal `input('Are you sure you want to quit? y/n')` or something simple like that – Rolv Apneseth Jan 21 '21 at 12:13
-
That's I know I can do that. But If user by mistake click on that cross button or try to close the program. Then a error should show. Either graphically or in written in console. I know little bit of using tkinter but still learning to make fully functional software with it. So If there any way than please help me. Also is there any module or function which detect that if user have click on that cross button like in pygame. But in pygame that won't work for console – Harish Kumawat Jan 21 '21 at 12:27
-
Does this answer your question? [Detect when the "x" or close button is pressed](https://stackoverflow.com/questions/37764489/detect-when-the-x-or-close-button-is-pressed) – Ibrahim Jan 21 '21 at 12:32
1 Answers
This can be done with the signal
library. This is a built in library that can asynchronously handle user events in Python. You can read more in the Python Documentation.
Here is a simple example that I typically use to remind myself. (As a note, this was inspired by this SO post from eight years ago):
import signal
import sys
def yay():
x = 0
while True:
x += 1
print(x)
def terminate_catcher(signum, frame):
#Restoring sigint in case user hits Ctrl+C for real to avoid loop
signal.signal(signal.SIGINT, actual_sigint)
#Some conditional
if input("Are you sure? (Y/N)") == "Y":
sys.exit(1)
#Restore the signal catcher if the user decides not to cancel
signal.signal(signal.SIGINT, terminate_catcher)
if __name__ == '__main__':
# store the original SIGINT handler
actual_sigint = signal.getsignal(signal.SIGINT)
signal.signal(signal.SIGINT, terminate_catcher)
yay()
Essentially, the main function registers an event handler that is called whenever the SIGINT
interrupt is called. Whenever the function runs, it restores the handler back to the original SIGINT
handler in case the user hits ^C
one more time while the exception is being handled so we can avoid an infinite loop. Whenever the function is triggered, it just asks the user if they want to leave and uses system.exit(1)
if they say yes. If the user decides to stay, it restores the handler for future events.

- 932
- 3
- 17