How can i execute function when user close the console window?
I want make something such this:
if close == True:
print("Really you want quit?")
option = input()
if option == "Y":
quit()
else:
pass
Here's how to add statements, and use a better quit() statement.
import sys # add this to your imports
anotherFile = "afterExit.py" #make sure this is in the same directory
def on_close():
print("Really you want quit?")
option = input()
if option == "Y":
os.system("python " + anotherFile) # on Linux/ Mac, use python3
sys.exit()
# i prefer sys.exit as it does not display the kill message box
else:
print("you chose not to quit")
Related
Hello everyone im fairly new to python and this is my first language im learning so far. Im making a menu using a while true loop that takes input to select whichever flashing method a person needs to use and after calling whatever file using subprocess.popen or subprocess.call and it finishes it goes back into the menu loop but does not respond to any input at all.
import sys
import time
import subprocess
#!/bin/bash
sys.path.append
menu = True
def mainMenu():
while menu:
print("""
##################
# Utilities Menu #
##################
1. Tray Flasher
2. ESP Burner
3. Quit
""")
choice = (input('Select a utility: '))
if choice == '1':
trayFlasher()
elif choice == '2':
espBurner()
elif choice == '3':
print('Goodbye!')
def trayFlasher():
print('Have Fun!')
time.sleep(1)
subprocess.call(["<path to file>"])
def espBurner():
print('Burn Baby Burn...')
time.sleep(1)
subprocess.Popen(['<path to file>'])
sorry if my code is not so good or efficient, currently still learning. TIA!
is there a way on IDLE to close the users shell if they were to enter something that would be supposed to "quit"?
You can use exit(0) to terminate the program, as follows:
while True:
i = input()
if i == "quit":
exit(0)
else:
# do something
pass
For more information about what 0 for exit() means:
Difference between exit(0) and exit(1) in Python
I'm looking to write a program that asks for user_input on a cyclical basis, but does not halt further procedures while waiting for the user to give input.
Is there a command that acts as a press of the RETURN key? Does this have to do with writing to stdin? An alternative might be to see if there is no user_input that currently exists.
Imagine operating the code below, where the program is constantly running, even if the user provides no input,
import os
import time
import termios
import sys
x = 0
while x < 60:
time.sleep(0.5)
x += 1
print x
user_input = raw_input(" ")
if user_input == "exit":
os._exit(1)
I have a little Python program that uses keyboard input to run certain commands.
I setup everything in one main program loop but now I'm wondering, do I even need a main loop?
Here is what I mean:
mainProgramLoop = 1
while mainProgramLoop == 1:
print ("\nType in the command. ")
keyBoardInput= input("command:")
if keyBoardInput == "a":
#do this
elif keyBoardInput == "b":
#do this
Do I actually need that while loop?
Thanks!
No, you do not need a main loop if you use the cmd.Cmd class included with Python:
#! /usr/bin/env python3
import cmd
class App(cmd.Cmd):
prompt = 'Type in a command: '
def do_a(self, arg):
print('I am doing whatever action "a" is.')
def do_b(self, arg):
print('I am doing whatever action "b" is.')
if __name__ == '__main__':
App().cmdloop()
The documentation for the cmd module includes an example near the bottom to help get you started.
So i want this to run when number 7 is entered it should quit the current program. I have tried with the rest of my menu selection but I just want to fix the quit at the moment.
if menu_selection == "7":
quit()
I am assuming you are guessing quit() is the way to exit.
You should try sys.exit():
import sys # at the head of your program
...
your program
...
if menu_selection == "7":
sys.exit()
If you want to exit your program you can use exit() from sys
import sys
[...]
if menu_selection == "7":
sys.exit()