我在下面的代码中得到'NoneType'对象不是可迭代的TypeError.下面的代码是使用pyautogui滚动数字文件夹中的10个图像(名称为0到9,以图像中的#命名),当它找到一个时,报告x的值以及它找到的数字.然后按x值对字典进行排序,以读取图像中找到的数字.
问题:我还在学习Python,这个TypeError让我st脚,我怎么能纠正这个?
#! python3
import sys
import pyautogui
# locate Power
found = dict()
for digit in range(10):
positions = pyautogui.locateOnScreen('digits/{}.png'.format(digit),
region=(888, 920, 150, 40), grayscale=True)
for x, _, _, _ in positions:
found[x] = str(digit)
cols = sorted(found)
value = ''.join(found[col] for col in cols)
print(value)
Run Code Online (Sandbox Code Playgroud)
回溯错误:
Traceback (most recent call last):
File "C:\Users\test\python3.6\HC\power.py", line 10, in <module>
for x, _, _, _ in positions:
TypeError: 'NoneType' object is not iterable
Run Code Online (Sandbox Code Playgroud)
你需要在None之前的迭代中添加检查positions
#! python3
import sys
import pyautogui
# locate Power
found = dict()
for digit in range(10):
positions = pyautogui.locateOnScreen('digits/{}.png'.format(digit), region=(888, 920, 150, 40), grayscale=True)
if positions is not None:
for x, _, _, _ in positions:
found[x] = str(digit)
cols = sorted(found)
value = ''.join(found[col] for col in cols)
print(value)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
36199 次 |
| 最近记录: |