2

While making an HTML help tool in Python, I wanted to exit the program smoothly, like clicking the X button on Google Chrome. But, I encountered an issue. It asks me if I want to kill the program, instead of doing it automatically.

I tried using quit(), exit() and sys.exit(). All do the same thing. How can I get the program to exit smoothly?

Clíodhna
  • 818
  • 1
  • 15
  • 29
EgMusic
  • 136
  • 17
  • 1
    Hello. Please show what you tried. – spectras Sep 04 '17 at 14:51
  • 1
    Please provide some example code. – SitiSchu Sep 04 '17 at 14:55
  • took a look at the other answers on stack overflow? https://stackoverflow.com/questions/38028151/how-do-i-get-rid-of-the-the-program-is-still-running-are-you-sure-you-want-to e.g. maybe this makes it clear to you. quit and exit are not to use. and with sys.exit() the message only appears in the idle I guess – Matthias Burger Sep 04 '17 at 15:03
  • print("Sorry, I didn't catch that..") print("-----YOU HAVE NO TOKENS LEFT-----") print("SHUTTING DOWN...") time.sleep(2) quit() – EgMusic Sep 04 '17 at 15:46

2 Answers2

2

As it was suggested in the comments, your problem should only be noticed inside Python's IDLE, but should run just fine when executed inside a terminal. However, this code should also kill your program in IDLE:

import os, signal
from time import sleep

print("I will sleep for 3 secs and shut down!")
sleep(3)

os.kill(os.getpid(), signal.SIGTERM)

This sends a signal to your application to terminate.

Or alternatively you could call os' _exit function. From the docs:

Exit the process with status n, without calling cleanup handlers, flushing stdio buffers, etc.

Stam Kaly
  • 668
  • 1
  • 11
  • 26
  • It worked! Although on the version I am using, there is no module named "sleep", I had to use `import time` and the command I had to use was `time.sleep(3)` – EgMusic Sep 04 '17 at 16:39
  • Thanks for the help! – EgMusic Sep 04 '17 at 16:39
  • if it worked, accept the answer by clicking the gray tick placed right below the arrow pointing down next to my answer. – Stam Kaly Sep 04 '17 at 16:43
0

I know it's late but I took a different way. It might be a dirty way of doing it. I don't like the accepted answer because you have to hardcode those lines in every script. I just comment out the lines, so that the dialog never shows on screen.

You can find the file /usr/lib/python3.9/idlelib/pyshell.py aprx: line 1007

def close(self):
    "Extend EditorWindow.close()"
    #if self.executing:
        #response = messagebox.askokcancel(
            #"Kill?",
            #"Your program is still running!\n Do you want to kill it?",
            #default="ok",
            #parent=self.text)
        #if response is False:
            #return "cancel"
    self.stop_readline()
    self.canceled = True
    self.closing = True
    return EditorWindow.close(self)

That way it will never ask you this silly question "Your program is still running!\n Do you want to kill it?"

Module_art
  • 999
  • 2
  • 9
  • 26