'Tuple'对象不可调用-Python

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

我正在使用pygame,并且正在使用一个函数来设置文本的选定位置在PyGame中:

def textPos(YPos , TextSize):
    TextPosition.center(60,YPos)
    print("Size : " + TextSize)

但是当我运行程序时,出现错误:

TextPosition.center(60,YPos) : TypeError : 'Tuple' object is not callable 

有解决此问题的方法吗?

python object pygame tuples callable
1个回答
1
投票

'Tuple'对象不是可调用错误,表示您正在将数据结构视为函数,并尝试在其上运行方法。 TextPosition.center是一个元组数据结构,而不是一个函数,您将其称为方法。如果尝试访问TextPosition.Center中的元素,请使用方括号[]

例如:

foo = [1, 2, 3]
bar = (4, 5, 6)

# trying to access the third element with the wrong syntax
foo(2) --> 'List' object is not callable
bar(2) --> 'Tuple' object is not callable

# what I really needed was
foo[2]
bar[2]
© www.soinside.com 2019 - 2024. All rights reserved.