首页 > 解决方案 > Python3.6 和 Pyautogui - 像素命令 - TypeError: __new__() 采用 4 个位置参数,但给出了 5 个

问题描述

我对 Python 比较陌生,我正在尝试使用PyautoguiPython3.6制作一个有趣的小项目。这个项目的目标是创建一个 TensorFlow 驱动的项目来学习下棋。我现在遇到的问题是,每当我尝试使用任何像素命令 - 例如 pyautogui.pixel(...) 或 screenshot.getpixel(...) - 都会弹出一个错误并说:

Traceback (most recent call last):
  File "main.py", line 109, in <module>
    Score()
  File "main.py", line 100, in Score
    if gui.pixelMatchesColor(tempX, tempY, (87, 83, 82), tolerance=20):
  File "/Users/student/Library/Python/3.6/lib/python/site-packages/pyscreeze/__init__.py", line 413, in pixelMatchesColor
    pix = pixel(x, y)
  File "/Users/student/Library/Python/3.6/lib/python/site-packages/pyscreeze/__init__.py", line 436, in pixel
    return RGB(*screenshot().getpixel((x, y)))
TypeError: __new__() takes 4 positional arguments but 5 were given

我只想指出所有其他命令都可以正常工作,并且只有像素命令不起作用。

我已经安装了所有东西,包括 pyautogui(duh)、pyscreez、pymsgbox、pytweening、xlib、opencv 和我能想到的任何其他包。这些是使用以下命令安装的:pip3 install package-name-here --user。我需要--user因为我目前对我的计算机没有管理员权限,所以我想知道这是否与我目前的困境有关。

我在寻找答案时也遇到了较早的帖子,但我忘记了在哪里找到它,所以很抱歉,但我无法链接它,但基本上它说我应该检查并删除所有pycache文件夹。我使用 ~/Library/Python/3.6 文件夹中的终端命令执行此操作:

find . -name "__pycache__" -type f -o -name "__pycache__" -type d -exec rm -rf {} \

我不需要一个确切的解决方案来解决这个问题,但我想知道是否有某种方法可以使用 pyautogui.pixelMatchesColor(...) 函数或您推荐的任何类似的函数,它可以具有容差 - 例如RGB 值可以相差 10 个单位并且仍然返回true

对于那些感兴趣的人,这是我的完整代码:

#
# IMPORT ALL NECESSARY THINGS
#

import os, time, sys, pyautogui as gui, argparse as arg

#
# FAILSAFES
#

gui.FAILSAFE = True
gui.PAUSE = 0.1

#
# SET UP ARGUMENT PARSER
#

parser = arg.ArgumentParser(description='A machine learning script powered by TensorFlow designed to be run on "chess.com" using Python.')
parser.add_argument("-s", "--sleep", nargs='?', type=int, default='5', help='Number of seconds that the program should sleep before starting. This gives you time to move over to the website before the program looks for the gamboard on screen.')
args = parser.parse_args()

#
# ASKS USER FOR WHAT SIDE IT IS ON
#

side = input("Are you white or black?  ")

if side == "W" or side == "w" or side == "white" or side == "White":
     side = "W"
elif side == "B" or side == "b" or side == "black" or side == "Black":
    side = "B"
else:
    print("Invalid selection for which side!")
    side = None
    sys.exit(0)

#
# PRINT "READY" AND THEN WAIT FOR SPECIFIED AMOUNT OF TIME - DEFAULT 5 SECONDS
#

print("Ready! Waiting for " + str(args.sleep) + " seconds!")
time.sleep(int(args.sleep))

#
# GET AREA OF GAMEBOARD ON SCREEN
#

if side == "W":
    gameboard = gui.locateOnScreen('./img/white/chessboard_white.png', confidence=0.55, grayscale=True)
    left = gameboard.left - 10
    top = gameboard.top - 5
    right = gameboard.left + gameboard.width + 10
    bottom = gameboard.top + gameboard.height + 15
elif side == "B":
    gameboard = gui.locateOnScreen('./img/black/chessboard_black.png', confidence=0.55, grayscale=True)
    left = gameboard.left - 10
    top = gameboard.top - 5
    right = gameboard.left + gameboard.width + 10
    bottom = gameboard.top + gameboard.height + 15

widthInterval = (right - gameboard.left) / 8
heightInterval = (bottom - gameboard.top) / 8

#
# DEFINES A FUNCTION THAT COUNTS THE SCORE 
# - NUMBER OF YOU SIDE AND THEN SUBTRACT THE NUMBER OF OPPOSITE SIDE
#

def Score():
    for i in range(8):
        for j in range(8):
            tempX = 32 + (i * widthInterval)
            tempY = 32 + (j * heightInterval)
            if gui.pixelMatchesColor(tempX, tempY, (87, 83, 82), tolerance=20):
                print("True!")
    
    if side == "W":
        print("White!")
    elif side == "B":
        print("Black!")


Score()

注意:问题出现在上述代码的最后 10 行。

非常感谢您的帮助,如果您需要我提供更多信息,请随时告诉我!最大限度

标签: pythonpython-3.xvisual-studiopython-3.6pyautogui

解决方案


推荐阅读