キーボードの入力からtimerを止めたいと考えています.
以下のコードでは1を押すとtimerがスタートしますが,ストップ用に用意した0のを押してもtimerが止まりません.
キーボードのイベントからtimerを止める方法をお教えいただければ幸甚です.
よろしくおねがいします.
python
1import threading 2import time 3#タイマークラス 20カウント 4class TimerClass(threading.Thread): 5 def __init__(self): 6 threading.Thread.__init__(self) 7 self.event = threading.Event() 8 self.count = 20 9 10 def run(self): 11 while self.count > 0 and not self.event.is_set(): 12 print (self.count) 13 self.count -= 1 14 self.event.wait(1) 15 16 def stop(self): 17 self.event.set() 18 19 20tmr = TimerClass() 21 22#キーボード入力待ち 23inputNum = input() 24if int(inputNum) == 1: 25 print("入力は1ですね") 26 #timer start 27 tmr.start() 28 time.sleep(3) 29 30 inputNum = input() 31elif int(inputNum) == 0: 32 print("入力は0ですね") 33 ######################### 34 #timer stopしてほしい 35 tmr.stop() 36 time.sleep(3) 37 inputNum = input() 38 39else: 40 print("入力はその他ですね") 41 inputNum = input() 42
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2017/05/12 12:45