简体   繁体   English

按键时的 Python3 切换脚本卡住了

[英]Python3 toggle script on key press stuck

The goal of this script is to make a switch and let the user toggle it ON and OFF with a key press, and when the switch is ON, the script should execute a loop that print a message in the terminal.该脚本的目标是制作一个开关,让用户通过按键将其打开和关闭,当开关打开时,脚本应执行一个循环,在终端中打印一条消息。 In another words, The goal is to repeatedly print a message when the switch is ON换句话说,目标是在开关打开时重复打印一条消息

Here is what i have tried:这是我尝试过的:

import keyboard
import time

switch = 0    # The switch variable


def check_start():
    global start
    if keyboard.is_pressed("F5") and start == 0:    # If F5 is pressed, turn ON print message loop
        switch = 1
        print(switch)
        time.sleep(0.1)    # This is to prevent the user toggling the switch too fast

    if keyboard.is_pressed("F5") and start == 1:     # If F5 is pressed again, turn OFF print message loop
        switch = 0
        print(switch)
        time.sleep(0.1)


def print_name():    # If the switch is ON, it should print two seperate message with a 10 seconds interval
    if start == 1:
        print("start")
        time.sleep(10)
        print("end")


while True:
    check_start()
    print_name()

Here is the output of the script:这是脚本的 output:

1
start
end
start
end
0
1
start
end
0

Now here's the problem:现在问题来了:

The user cannot turn off the switch while the print loop is in progress.当打印循环正在进行时,用户不能关闭开关。 For example the user cannot turn off the script if the message "end" has not printed, the user can ONLY turn off the switch exactly after "end" has printed, and if the user has missed the opportunity to turn off the switch, he must wait 10 more seconds to turn it off.例如,如果消息“end”还没有打印,用户不能关闭脚本,用户只能在“end”打印完后关闭开关,如果用户错过了关闭开关的机会,他必须再等 10 秒才能将其关闭。 What i expected is the user can toggle On and OFF anytime s/he wishes to.我所期望的是用户可以在他/她希望的任何时候打开和关闭。

Is this possible to do in without importing too much module?这可以在不导入太多模块的情况下完成吗?

you can put an if condition inside your loop to do a break at a specific key您可以在循环中放置一个 if 条件以在特定键处中断

ex:前任:

if keyboard.is_pressed("F5") and start == 1:
    break

this would exit your infinite loop, although a more elegant code would work like this:这将退出您的无限循环,尽管更优雅的代码会像这样工作:

def check_start():
    global start
    if keyboard.is_pressed("F5") and start == 0:
        #DO stuff
        return True

    if keyboard.is_pressed("F5") and start == 1:
        #DO stuff
        return False


while check_start():
    print_name()

Here is a cleaner, more reliable approach using keyboard's built-in add_hotkey method.这是使用键盘内置的 add_hotkey 方法的更简洁、更可靠的方法。

Below is a simple program to toggle printing of the current timestamp.下面是一个简单的程序来切换当前时间戳的打印。

import keyboard as kb
from datetime import datetime as dt

global print_flag
print_flag = False

def toggle_print():
    global print_flag
    print_flag = not print_flag

kb.add_hotkey('d', toggle_print)

while True:
    timestamp = dt.now()
    if print_flag:
        print(timestamp)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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