'Tuple' 对象不可调用 - Python
'Tuple' object is not callable - Python
我正在使用 pygame 并且我正在使用一个函数来设置文本的选定位置
在 PyGame 中:
def textPos(YPos , TextSize):
TextPosition.center(60,YPos)
print("Size : " + TextSize)
但是当我运行这个程序时,我得到一个错误:
TextPosition.center(60,YPos) : TypeError : 'Tuple' object is not callable
有办法解决这个问题吗?
'Tuple' object is not callable 错误意味着您正在将数据结构视为函数并尝试 运行 其上的方法。 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]
我正在使用 pygame 并且我正在使用一个函数来设置文本的选定位置 在 PyGame 中:
def textPos(YPos , TextSize):
TextPosition.center(60,YPos)
print("Size : " + TextSize)
但是当我运行这个程序时,我得到一个错误:
TextPosition.center(60,YPos) : TypeError : 'Tuple' object is not callable
有办法解决这个问题吗?
'Tuple' object is not callable 错误意味着您正在将数据结构视为函数并尝试 运行 其上的方法。 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]