![](/img/trans.png)
[英]How can I make a timer where I press the space button or whatever key on the keyboard to stop the timer?
[英]How can I make a button to stop the timer in my code?
import time
print("The timer on this project Will start")
#Ask To Begin
start = input('Would you like to begin learning now? (yes / no):')
if start == 'yes':
timeloop = True
#variable to keep the time
Sec = 0
Min = 0
#Begin process
timeloop = start
while timeloop:
Sec +=1
print(str(Min) + " Mins " + str(Sec) + " Sec ")
time.sleep(1)
if Sec == 60:
Sec = 0
Min +=1
print(str(Min) + " Minute ")
到目前為止,這是我的計時器程序,但我不確定如何讓它在命令提示符下啟動后停止? 我希望能夠按 X 並暫停代碼。 或者也許按 Y 然后它恢復,Z 只是一起關閉程序,但我不知道如何。
time.sleep 實際上停止執行代碼正在運行的當前進程。 如果您想保持時間並且仍然使進程響應,您可能需要使用線程來保持時間並安裝鍵盤 package 以使用keyboard.is_pressed()!
這篇文章真的很好: https://realpython.com/intro-to-python-threading/#what-is-a-thread
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.