【问题标题】:Restart While Loop With Keypress使用按键重新启动 While 循环
【发布时间】:2019-08-16 19:28:28
【问题描述】:

我希望在退出使用相同的键后通过按键重新启动 while 循环。基本上我正在寻找可以通过按键打开和关闭的while循环。到目前为止,我的代码通过按键停止了循环,但我不知道如何重新启动它。

import keyboard
from time import sleep

key = "right shift"

while True:
    print("Running")
    sleep(0.5)
    if keyboard.is_pressed(key):
        break

我会描述一下我在这里尝试过的事情,但老实说我不知道​​。

编辑:很抱歉一开始不够清楚,但这就是我要找的:

如果你想让循环重新开始,那么再放一个 while 循环 当前的,并在那个等待一个 在进入内部循环之前按下按键。

我已经按照 Kyle 的建议进行了操作,并且效果很好,只是您必须按住钥匙才能停止。我相信它可以通过时间来修复,这是我目前所拥有的:

import keyboard
from time import sleep

key = "right shift"

while True:
    if keyboard.is_pressed(key):
        while True:
            print("Running")
            sleep(0.5)
            if keyboard.is_pressed(key):
                    sleep(1)
                    break

【问题讨论】:

  • 等等,你想要切换还是重启?
  • 在函数中创建循环,重新启动将再次执行该函数。
  • @Axium 切换将是正确的词。按下键时循环停止,再次按下时循环开始。

标签: python while-loop keypress restart


【解决方案1】:

keyboard 模块具有更多功能,允许使用不同的钩子/阻塞器。

只需使用keyboard.wait(key) 来阻止控制流,直到按下key

import keyboard
from time import sleep

key = "right shift"

while True:
    print("Running")
    sleep(0.5)
    if keyboard.is_pressed(key):
        print('waiting for `shift` press ...')
        keyboard.wait(key)

交互输出示例:

Running
Running
Running
waiting for `shift` press ...
Running
Running
Running
Running
Running
waiting for `shift` press ...
Running
Running
Running
...

【讨论】:

    【解决方案2】:

    一种方法是设置一个标志:

    go = True
    while True:
      if go:
        print('Running...')
      sleep(0.5)
      if keyboard.is_pressed(key):
        go = not go
    

    但是,这不是编写此类代码的好方法,因为您的程序完全控制了处理器。当goFalse 时,这称为“忙等待”。相反,您应该了解 Kyle 在他的回答中描述的事件处理。

    【讨论】:

      【解决方案3】:

      如果我正确理解了您的问题,您希望运行 while 循环,直到用户按下键,然后 break 在您的代码中执行其他操作,直到再次按下键。

      一种选择是注册一个键盘事件处理程序,这样无论何时按下一个键,无论您在脚本中的哪个位置(只要它仍在运行),都会调用一些处理程序函数。

      然后将 while 循环放入一个函数中,并让该函数由键盘事件处理程序调用。该函数应首先禁用/取消注册事件处理程序,然后在退出前立即重新注册。这样,函数在运行时就不会再次被调用,而是在运行完成后再次响应。

      如果你只是想暂停循环,你可以在 if 块中放置另一个 while 循环,等待按键被按下,然后再让外部循环继续。

      如果您希望循环重新开始,则在当前循环周围放置另一个 while 循环,并在该循环中放置一行,等待按键按下,然后再进入内部循环。

      编辑:因为看起来你想要中间那个,这里有一个基本上使用你已经在使用的技术的例子:

      import keyboard
      from time import sleep
      
      key = "right shift"
      
      while True:
          print("Running")
          sleep(0.5)
          if keyboard.is_pressed(key):
              # May need a delay here to prevent it from immediately continuing
              while True:    # Will stop the loop until you press again
                  # Definitely put a delay here. Your CPU will thank you
                  if keyboard.is_pressed(key): # Check if key is pressed again
                      break;  # Get out of inner loop
              continue    # Go back to beginning of outer loop
                          # The continue is optional if this is the end of the loop
      
      

      编辑 2:最后一个示例(尽管如果您在我给出的第一个示例中包含 continue 会产生类似的效果)

      import keyboard
      from time import sleep
      
      key = "right shift"
      
      while True:
          while True:
              print("Running")
              sleep(0.5)
              if keyboard.is_pressed(key):
                  break # Gets you out of the original loop
          # Put this at the end of the inner loop 
          # if you don't want to have to hit the key to start
          while True:    # Will stop the loop until you press again
              # Definitely put a delay here. Your CPU will thank you
              if keyboard.is_pressed(key): # Check if key is pressed again
                          break;  # Get out of inner loop
      
      

      【讨论】:

      • 该死,很抱歉回复晚了,最后一个选项正是我要找的。我希望你没有在这段代码上浪费太多时间
      • 我更新了答案以包含另一个示例,并添加了关于@Code-Apprentice 建议的延迟的评论。如果你使用一个小的延迟,它对用户来说并不是特别明显,但会对你的程序尝试使用多少 CPU 时间产生巨大的影响。可能还有一个版本的keyboard.is_pressed() 函数可以阻塞直到按下一个键。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-10-16
      • 1970-01-01
      • 1970-01-01
      • 2017-11-25
      • 2021-03-11
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多