python-3.x py脚本在执行后停止

0pizxfdo  于 2023-01-14  发布在  Python
关注(0)|答案(1)|浏览(141)

我有以下脚本:

import time
from pynput import keyboard
from pynput.keyboard import Key, Listener

STATUS=False
COMBINATION={keyboard.Key.esc, keyboard.Key.alt}

# The currently active modifiers
current = set()

def on_press(key):
    if key in COMBINATION:
        current.add(key)
        if all(k in current for k in COMBINATION):
            STATUS = False
    if key == keyboard.Key.esc:
        # Stop listener
        STATUS = True

def on_release(key):
    try:
        current.remove(key)
    except KeyError:
        pass

def hp(delay):
    keyboard.press(Key.f1)
    keyboard.release(Key.f1)
    time.sleep(delay)

def cp():
    keyboard.press(Key.f2)
    keyboard.release(Key.f2)

def main():
    with Listener(
        on_press=on_press,
        on_release=on_release) as listener:
            listener.join()
    while True:
        while STATUS:
            for i in range(3):
                hp(0.5)
            cp()

当我用pyinstaller生成一个可执行文件,甚至用python3运行到最后,脚本不会像我想的那样循环(永远运行),因为我没有任何错误消息,我不知道我的错误可能在哪里,有人能帮忙吗?

knpiaxh1

knpiaxh11#

您需要为脚本创建一个入口点。请将其添加到文件底部,然后重试:

if __name__ == '__main__':
    main()

相关问题