“cara menangkap ctrl c di python” Kode Jawaban

Ctrl C Pengecualian Python

#Try the following:
try:
    # DO THINGS
except KeyboardInterrupt:
    # quit
    sys.exit()
Razoltheren

cara menangkap ctrl c di python

import signal
import time
import readchar
 
def handler(signum, frame):
    msg = "Ctrl-c was pressed. Do you really want to exit? y/n "
    print(msg, end="", flush=True)
    res = readchar.readchar()
    if res == 'y':
        print("")
        exit(1)
    else:
        print("", end="\r", flush=True)
        print(" " * len(msg), end="", flush=True) # clear the printed line
        print("    ", end="\r", flush=True)
 
 
signal.signal(signal.SIGINT, handler)
 
count = 0
while True:
    print(f"{count}", end="\r", flush=True)
    count += 1
    time.sleep(0.1)
Arsham

cara menangkap ctrl c di python

import signal
import time
 
def handler(signum, frame):
    res = input("Ctrl-c was pressed. Do you really want to exit? y/n ")
    if res == 'y':
        exit(1)
 
signal.signal(signal.SIGINT, handler)
 
count = 0
while True:
    print(count)
    count += 1
    time.sleep(0.1)
Arsham

Jawaban yang mirip dengan “cara menangkap ctrl c di python”

Pertanyaan yang mirip dengan “cara menangkap ctrl c di python”

Lebih banyak jawaban terkait untuk “cara menangkap ctrl c di python” di Python

Jelajahi jawaban kode populer menurut bahasa

Jelajahi bahasa kode lainnya