python: 类函数返回值但不可用。

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

我有一个叫做Stone的类,里面有一个叫做return_coordinates(self)的函数,用来返回石头的坐标。当我打印坐标时,我可以完美地看到它们,但当我试图使用它们时,它打印出错误。TypeError: 'module' object is not callable(模块对象不可调用)

这就是这个类。

class Stone:
    def __init__(self, image, x, y):
        self.image = image
        self.x = x
        self.y = y

    def draw_stone(self):
        image = pygame.image.load(self.image)
        screen.blit(image, (self.x, self.y))

    def return_id(self):
        return self.image.replace("image", "").replace(".png", "")

    def return_coordinates(self):
        return [self.x, self.y]

现在我创建了一些类Stone对象 并把它们存储在一个列表中. 当我访问列表中的对象并尝试使用它的函数return_coordinates()时,它打印出了错误。

下面是代码。

if draw_selected == True:
    coordinates = selected[3].return_coordinates()
    selection_box = pygame.rect(coordinates[0], coordinates[1], 57, 80)
    pygame.draw.rect(screen, [21, 146, 146], selection_box, 2)

但当我想访问列表中的一个特定坐标时 比如坐标[0],我就得到了错误信息。所有的代码都在同一个文件里。

有什么办法可以解决这个问题吗?

完整的错误信息。

Traceback (most recent call last):
File "C:\Users\Gabriel\Desktop\tech\project_m\mahjong.py", line 198, in <module>
selection_box = pygame.rect(coordinates[0], coordinates[1], 57, 80)
TypeError: 'module' object is not callable
python list function class
1个回答
0
投票

请注意,Python是一种区分大小写的语言。您的代码可以在以下情况下工作 pygame.Rect:

if draw_selected == True:
    coordinates = selected[3].return_coordinates()
    selection_box = pygame.Rect(coordinates[0], coordinates[1], 57, 80)
    pygame.draw.rect(screen, [21, 146, 146], selection_box, 2)
© www.soinside.com 2019 - 2024. All rights reserved.