如果按了某個鍵,我想暫停并繼續threads。我試過了:如果按q,它會刪除"time.sleep(99999)(改為0),但是沒用,有人能幫我嗎?
import keyboard
from threading import Thread
from time import sleep
Thread1 = True
Thread2 = True
class main():
def test1():
if keyboard.is_pressed("q"): #if keyboard is pressed q it will reomve the sleep
time = 0
time = 99999
while Thread1 == True:
print("Thread1")
sleep(time)
def test2():
while Thread2 == True:
print("Thread2")
sleep(1)
Thread(target=test1).start()
Thread(target=test2).start()
main()
您可以為此創建一個類
當用戶點擊
q
時,我們將調用stop()
函數。