只通过迭代数组的某个部分的正确方法?

问题描述 投票:-1回答:1
Traceback (most recent call last):
 File "C:/Users/yaahy/PycharmProjects/Testing/testing1.py", line 45, in 
<module>
    clicktheshit()
  File "C:/Users/yaahy/PycharmProjects/Testing/testing1.py", line 41, in 
clicktheshit
    pyautogui.click(chords[0], chords[1])
TypeError: 'NoneType' object is not subscriptable

因为我的脚本运行缓慢搜索每个像素,我想通过切掉它看到的一些无用的像素(不在游戏区域中的那些)来加速它,但是使用

pxlss = pxls[60:400] 

不起作用,我不知道这个问题,因为它没有试图削减无用的东西,但它只是很慢

import pyautogui
import time
from PIL import Image
import mss
import mss.tools
import cv2
import numpy as np
from PIL import ImageGrab
import colorsys

time.sleep(2)

def shootfunc(xc, yc):
    pyautogui.click(xc, yc)

gameregion = [71, 378, 328, 530]

def findpixels(pxls):
    pxlss = pxls[60:400]
    for row, pxl in enumerate(pxlss):
        for col, pxll in enumerate(pxl):
            if col >= 536 and col <= 808 and row <= 515 and row >= 371 and pxll == (102, 102, 102):
                foundpxl = pxll
                print(str(col) + " , " + str(row))
                return [col, row]
                break


def clicktheshit():
    with mss.mss() as sct:
        region = {'top': 0, 'left': 0, 'width': 1920, 'height': 1080}
        imgg = sct.grab(region)
        pxls = imgg.pixels
        chords = findpixels(pxls)
        pyautogui.click(chords[0], chords[1])

xx = 0
while xx <= 3000:
        clicktheshit()
        xx = xx + 1
        time.sleep(.01)
        clicktheshit()
python indexing
1个回答
3
投票

阅读错误消息和回溯应该给你第一个提示:

File "C:/Users/yaahy/PycharmProjects/Testing/testing1.py", line 41, in clicktheshit
pyautogui.click(chords[0], chords[1])
TypeError: 'NoneType' object is not subscriptable

这意味着在这个确切的行中,chordsNone对象 - 当然不能被索引 - 而不是你期望的[col, row]列表。

现在为什么你得到这个None而不是预期的列表很简单:你的findpixels函数只返回这个列表,如果它实际找到匹配 - 否则,函数最终没有明确的return语句,所以它隐含地返回None

IOW,你的问题与“仅仅迭代数组的某个部分的正确方法”无关......而且与不知道如何调试程序有很大关系。

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