Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/358.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 有没有办法在不暂停整个程序的情况下模拟time.sleep()函数的效果?_Python_Python 3.x_Time_While Loop - Fatal编程技术网

Python 有没有办法在不暂停整个程序的情况下模拟time.sleep()函数的效果?

Python 有没有办法在不暂停整个程序的情况下模拟time.sleep()函数的效果?,python,python-3.x,time,while-loop,Python,Python 3.x,Time,While Loop,我是一个新的Python3.9用户,正在尝试提高我的编码。我正在编写一个程序,它使用while循环,并根据用户输入调用函数来按键。我的代码基本上如下所示: from pynput.keyboard import Key, Controller from graphics import * import time def wack(): M = Controller() for i in range(3): M.press('a') time.slee

我是一个新的Python3.9用户,正在尝试提高我的编码。我正在编写一个程序,它使用while循环,并根据用户输入调用函数来按键。我的代码基本上如下所示:

from pynput.keyboard import Key, Controller
from graphics import *
import time
    
def wack():
   M = Controller()
   for i in range(3):
      M.press('a')
      time.sleep(1)    # This guy right here is the issue
      M.release('a')

def main():
    click = Win.checkMouse()
    while not click(click, exit_box):
       click = Win.checkMouse()
       if *input* == *correct string*:
          wack()
       
我有一个带有退出按钮的图形窗口,当单击退出时,它会终止程序并关闭窗口。问题在于
time.sleep(1)
不会暂停输入或代码中的那个点,它会暂停整个程序,暂停while循环,这不是我想要它做的


搜索时间函数没有提供任何明确的解决方案,因为
time.sleep()
似乎是python中最流行的暂停函数。

Async await是python3.7中添加的一个惊人的特性,它允许您在程序其余部分执行时等待对象或函数得到结果

Async await有一个sleep方法,它只暂停异步函数,并让程序中的其余代码执行

就是这样:

from pynput.keyboard import Key, Controller
from graphics import *
import time
import asyncio

#This is the asnychronous funtion    
async def wack():
   M = Controller()
   for i in range(3):
      M.press('a')
      await.sleep(1)#this sleeps the function but allows the rest of the program to work
      M.release('a')

def main():
    click = Win.checkMouse()
    while not click(click, exit_box):
       click = Win.checkMouse()
       if *input* == *correct string*:
          asyncio.run(wack()) #calling the asynchronous funtion
       

这是一个关于理解python的教程

我读了几遍,但有点模糊。当你说“图形窗口”时,你是指电子游戏,比如说你想做一个机器人吗?另外,对于time.sleep,它所做的只是在程序中暂停,因此在wack函数中的for循环中,所有sleep所做的就是在该行上暂停,这就是所有time.sleep所做的。它在该行代码处休眠一段预定的时间,然后再次启动,如果您不尝试这样做,那么使用它是不正确的。因此,您在这一点上从头到尾想要实现的目标可能会更有帮助。@NewCoder18从头到尾,这段代码接受用户以预设代码的形式输入。然后,该代码激活并持续按下输入按钮。这一切的目的是成为一名教练,我可以在一个模拟的口袋妖怪游戏中使用。因此,用户单击程序创建的图形窗口,输入他们的代码,当代码在后台工作时,仍然可以与窗口交互。