简体   繁体   中英

How do I run a function alongside my main code?

def explain():
    Timer = 60
    keyboard.press("/")
    time.sleep(0.1)
    keyboard.write(StillInBeta)
    time.sleep(0.8)
    keyboard.press('enter')
    time.sleep(0.8)
    keyboard.press("/")
    time.sleep(0.1)
    keyboard.write(Reminder)
    time.sleep(0.8)
    keyboard.press('enter')
    time.sleep(0.1)
    for x in range(60):
        Timer = Timer - 1
        time.sleep(1)
        print(Timer)
Explain_And_Remind = threading.Thread(target=explain())
Explain_And_Remind.join()

I've tried .start(), .run(), and .join(). Searching gave me nothing. I'm not sure how to go about doing this. I'm running all code in a while true statement.

This is a very common mistake, even experienced people also miss these paranthesis.

The target argument of the thread takes a function object. For example -

def greet():
    print('Hello!')

Here greet is a function object, whereas greet() calls the function.

In your case, you are calling the function, which is not intended.

You have to pass the function object, threading.Thread(target=explain)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM