【问题标题】:'Tuple' object is not callable - Python“元组”对象不可调用 - Python
【发布时间】:2020-08-29 16:04:31
【问题描述】:

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

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

但是当我运行程序时,我得到一个错误:

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

有办法解决这个问题吗?

【问题讨论】:

  • 什么是TextPosition?是pygame.Rect 吗?如果是,那么它必须是TextPosition.center = (60,YPos)
  • TextPosition 在哪里定义?它是pygame 的一部分吗?我找不到它的参考。
  • textToWrite = font.render("MyText", True,white,black) TextPosition = textToWrite.get_rect()
  • @Rabbid76 回答了您的问题。既然我们知道 TextPosition 确实是 rect() 对象,那么最好给出一个答案。

标签: python object pygame tuples callable


【解决方案1】:

'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]

【讨论】:

  • 当一个问题不完整时,你怎么能给出答案?您的回答与问题并不完全相关。
  • 是的,非常糟糕的做法。如果你不是新人,我会否决你。 @Roshan 是对的。此外,在对 Q 的第一条评论中已经给出了答案。
  • 我终于有了解决方案。现在可以了,谢谢!
猜你喜欢
  • 2023-03-27
  • 2021-09-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-06-15
相关资源
最近更新 更多