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

2024-05-04 20:30:28 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图用库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条件以完成此任务

还有其他更简单的方法吗


Tags: importnoneifisosnotbuttonsome
1条回答
网友
1楼 · 发布于 2024-05-04 20:30:28

我不知道为什么要使用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, '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")
              return some_button

#  - main  -

pos = button_detector()

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

或者使用更少的printssleep,因为代码可能运行得太快

import pyautogui
import os
import time

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:
              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, region=(0, 510, 547, 153), 
                                    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'), 
                               region=(0, 510, 547, 153), 
                               grayscale=False, 
                               minSearchTime=3)


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

相关问题 更多 >