首页 > 解决方案 > 如何使用 pyautogui 简化 while 循环以实现无限循环

问题描述

我已经潜伏了一段时间,但是我看到的所有现有帖子都无法帮助我。我目前正在自学python,所以如果这是一个我没有看到的简单修复,我深表歉意。

我对这段代码的目标是使用 pyautogui.hotkey 循环浏览浏览器中的选项卡。它需要用户输入循环通过的选项卡数量,并执行 pyautogui 命令。

但是,我的问题是我无法创建循环 for 或 while 循环。

我玩过should_restart变量for i in range(x)等,但我只是没有看到我的修复。

下面的代码本质上是我想一起简化的。

我理想化的流程是:

input-> 增加tabcounter1 直到等于input-> 重置tabcounter-> 冲洗并重复。

numberofTabs = input('How many tabs do you have? \n')

tabcounter = 0

while int(tabcounter) < int(numberofTabs):
    tabcounter = tabcounter+1
    pyautogui.hotkey('alt', str(tabcounter))
    break

while int(tabcounter) == int(numberofTabs):
    tabcounter = 0

我希望这段代码循环直到我中断它,或者很长一段时间。

先感谢您。感谢您的帮助!

编辑:在修改它并将我的代码包装在一个循环中之后,我想出了这个:

loopcount = input('How many times do you want this to loop?')

time.sleep(5)

count = 0
for i in range(int(loopcount)):
    while count < int(numberofTabs):
        count += 1
        pyautogui.hotkey('alt', str(count))
        time.sleep(1)
    else:
        count = 0```

标签: pythonwhile-looppyautogui

解决方案


推荐阅读