[我试图让我的Python代码在不符合条件的情况下单击按钮

问题描述 投票:0回答:1

目前,只要满足条件,它就会发出点击。

例如:

该代码在我的屏幕上寻找一个特定的单词。它与我已经定义的一组数据匹配。如果没有匹配项,请在屏幕上单击。如果有匹配项,请告诉我什么是匹配项,然后退出。

现在,一旦满足匹配条件,它将单击一下,然后退出代码,但我不知道为什么。

下面的代码:

import pytesseract
import numpy as nm
import winsound
import pyautogui
import time
from PIL import ImageGrab

def imToString():
    pytesseract.pytesseract.tesseract_cmd = r'C:\\Program Files\\Tesseract-OCR\\tesseract.exe'
    while (True):

        source = [
"chocolate", "muffin", "cookie"
                 ]

        cap = ImageGrab.grab(bbox=(748, 626, 916, 646))

        tesstr = pytesseract.image_to_string(
            cv2.cvtColor(nm.array(cap), cv2.COLOR_BGR2GRAY),
            lang='eng')

        split_words =tesstr.split()

        normalized_input = [word.rstrip('.,!?').lower() for word in split_words]
        source_normalized = [word.lower() for word in source]

        set_a = set(source_normalized)
        set_b = set(normalized_input)
        match = set_a & set_b

        if match:
            print(match)
            frequency = 2500  # Set Frequency To 2500 Hertz
            duration = 1000  # Set Duration To 1000 ms == 1 second
            winsound.Beep(frequency, duration)
            return False
        else:
            print("No common elements")
            time.sleep(1)
            pyautogui.moveTo(826,309)
            pyautogui.click()


imToString()

关于我犯什么错误的任何信息?

python python-3.x web python-tesseract pyautogui
1个回答
0
投票

我猜问题在这里:

if match:
    print(match)
    frequency = 2500  # Set Frequency To 2500 Hertz
    duration = 1000  # Set Duration To 1000 ms == 1 second
    winsound.Beep(frequency, duration)
    # the while true stops here, you don't have the time to see the answer
    return False

也许添加:

input("Press Enter to continue...")

返回之前。

© www.soinside.com 2019 - 2024. All rights reserved.