尝试更新PIL.ImageGrab,但无法正常工作

时间:2019-07-01 20:24:37

标签: python-3.x automation

我正在尝试编写一个代码,该代码需要按下一个按钮。

一个功能将告诉您要按下哪个按钮。它将检查按钮是否为有效按钮(1到4),然后找到导航器控件(从我尝试自动化的应用程序中自定义,我无法找到命令其执行特定选项的选项),请检查根据存储器中的PIL设置其颜色,如果有效,请按按钮并等待,直到按钮更改颜色以恢复代码。

from pywinauto import application
import pywinauto
import PIL
import time
from array import *
time.clock()

def sanity_check():
    if 'app' not in globals():
        global app
        app = application.Application()
        app.connect(title="Contas a pagar")
    if 'screen' not in globals():
        global screen
        screen = PIL.ImageGrab.grab()
    if 'colorsarr' not in globals():
        global colorsarr
        colorsarr = [[(36, 210, 71),(82, 94, 105)],[(198, 62, 62),(84, 96, 107)],[(174, 218, 201),(169, 187, 207)],[(229, 13, 0),(64, 73, 81)]]

def update_screen():
    screen = PIL.ImageGrab.grab()

def click_button(number=1):
    if 1 <= number <= 4:
        number = int(number)
        buttonCoord = int((app.Contas_a_pagar.TBDBNavigator2.rectangle().right - app.Contas_a_pagar.TBDBNavigator2.rectangle().left)/4/2), int((app.Contas_a_pagar.TBDBNavigator2.rectangle().bottom - app.Contas_a_pagar.TBDBNavigator2.rectangle().top)/2)
        buttonCoord = app.Contas_a_pagar.TBDBNavigator2.rectangle().left + buttonCoord[0]*(number*2-1),buttonCoord[1]+app.Contas_a_pagar.TBDBNavigator2.rectangle().top
        if screen.getpixel(buttonCoord) == colorsarr[number-1][0]:
            pywinauto.mouse.click(button='left', coords=(buttonCoord))
            while screen.getpixel(buttonCoord) != colorsarr[number-1][1]:
                update_screen()
                continue
            return
        elif screen.getpixel(buttonCoord) == colorsarr[number-1][1]:
            print("Button unavailable")
            return
        else:
            print("Color check button " + str(number))
            return

def automate_things():
    sanity_check()
    print(time.clock())
    for i in range(-3,10):
        click_button(i)
    print(time.clock())

automate_things()

我的问题是我的函数update_screen应该更改存储在var screen中的内容,但事实并非如此,这会使while循环变为无限。即使我将窗口放在它抓住像素颜色的位置,它也会不断获得其他颜色。

colorsarr数组在0索引上具有“可按下”的颜色,在1索引上具有“不可按下”的颜色

这是我的第一个python代码,所以我可能做错了什么。完成后,我将尝试代码审查以获得更好的代码。欢迎提出更好的编码建议,但重点只是为什么屏幕var不更新。

0 个答案:

没有答案
相关问题