如何打破python中自定义类方法的while条件?

问题描述

我正在尝试使用库 pyautogui 检测按钮,想法是在检测到按钮时只打印迭代试验,当最终检测到按钮时,只需使用pyautogui.locateOnScreen() 并中断迭代。

但是,下一个代码将永远评估第一次迭代 (0) 并且永远不会评估后续的 if 条件:

import pyautogui
import os

def button_detector():
    for i in range(450):
        some_button=pyautogui.locateOnScreen(os.path.join(ROOT_DIR,r'some_button.png'),region=(0,510,547,153),grayscale=False)
            
        while some_button is None:
              print('Button not found at trial {}'.format(i))
              print('Error: did not find any button')
                     
              if some_button is not None:
                 print(some_button)
                 print("Button detected")
                 break

我怎样才能打破 while 条件来完成这个任务?

还有其他更简单的方法吗?

解决方法

我不知道你为什么使用 forwhile,但对我来说应该是 if/else 而不是 while 和嵌套的 if。>

import pyautogui
import os

def button_detector():
    path = os.path.join(ROOT_DIR,'some_button.png')
    
    for i in range(450):
          some_button = pyautogui.locateOnScreen(path,region=(0,510,547,153),grayscale=False)
        
          if some_button is None:
              print('Button not found at trial {}'.format(i))
              print('Error: did not find any button')
          else:       
              print(some_button)
              print("Button detected")
              break
 

但对我来说,它更需要 return some_button 而不是 break

import pyautogui
import os

def button_detector():
    path = os.path.join(ROOT_DIR,grayscale=False)
        
          if some_button is None:
              print('Button not found at trial {}'.format(i))
              print('Error: did not find any button')
          else:       
              print(some_button)
              print("Button detected")
              return some_button

# --- main ---

pos = button_detector()

if pos:
    print('button detected at position',pos)       
else:     
    print('button NOT detected')       

或者使用较少的 prints 并使用 sleep,因为代码可能运行得太快。

import pyautogui
import os
import time

def button_detector():
    path = os.path.join(ROOT_DIR,grayscale=False)
        
          if some_button:
              return some_button

          #time.sleep(0.1) # to wait little longer
          
# --- main ---

pos = button_detector()

if pos:
    print('button detected at position',pos)       
else:     
    print('button NOT detected')       
    

编辑:

我检查了 locateOnScreen 的源代码,它使用了函数 pyscreeze.locateOnScreen 有选项 minSearchTime - 所以如果你想等待更长时间的按钮 - 即。 3秒后即可使用

pyautogui.locateOnScreen(...,minSearchTime=3)

它会运行循环,它会一次又一次地检查它 3 秒钟 - 你不必创建自己的循环。

import pyautogui
import os

def button_detector():
    path = os.path.join(ROOT_DIR,'some_button.png')
    return pyautogui.locateOnScreen(path,grayscale=False,minSearchTime=3)

# --- main ---

pos = button_detector()

if pos:
    print('button detected at position',pos)       
else:     
    print('button NOT detected')       

或者更简单

导入pyautogui 导入操作系统

# --- main ---

pos = pyautogui.locateOnScreen(os.path.join(ROOT_DIR,'some_button.png'),minSearchTime=3)


if pos:
    print('button detected at position',pos)       
else:     
    print('button NOT detected')