简体   繁体   English

Python模块问题:TypeError:“模块”对象不可调用

[英]Python Module Issue: TypeError: 'module' object is not callable

I've been attempting to develop a text adventure type game in Python (and PyGame), and thus needed a module to repeatedly blit text to the screen. 我一直在尝试用Python(和PyGame)开发文本冒险类游戏,因此需要一个模块来将文本反复向屏幕显示。 After searching through a few, I downloaded KTextSurfaceWriter and installed it. 搜索了一些之后,我下载了KTextSurfaceWriter并安装了它。 Then I tried to follow the demo in the text provided here ( http://www.pygame.org/project-KTextSurfaceWriter-1001-.html ) 然后,我尝试遵循此处提供的文本中的演示( http://www.pygame.org/project-KTextSurfaceWriter-1001-.html

My code: 我的代码:

from ktextsurfacewriter import KTextSurfaceWriter

import pygame
from pygame.locals import *
import pygame.font
pygame.font.init()
screen = pygame.display.set_mode((640,480), 0, 32)

surface = pygame.surface ( (400, 400), flags = SRCALPHA, depth = 32)
surface.fill( (255,255,255,255) )

def blitSurface():
    screen.blit(surface, (50,50) )
    pygame.display.update()

blitSurface()

def waitForUserAction():
    while True:

        for event in pygame.event.get():
            if event.type == QUIT:
                import sys
                sys.exit()
            if event.type == KEYDOWN:
                return

waitForUserAction()

However, this throws back the module error at line 9. I'm fairly new to Python and most of the solutions I saw for this issue involved using the 'from [module] import' code that I already have at the beginning. 但是,这使模块错误返回到第9行。我对Python还是相当陌生,因此我针对该问题看到的大多数解决方案都涉及使用开始时已经具有的“ from [module] import”代码。

You are calling the pygame.surface module: 您正在调用pygame.surface模块:

surface = pygame.surface ( (400, 400), flags = SRCALPHA, depth = 32)

Either use pygame.surface.Surface() or use pygame.Surface() (note the capital S ); 使用pygame.surface.Surface()或使用pygame.Surface() (注意大写pygame.Surface() S ); these are both the same class but pygame.surface is the module in which it is defined. 这些都是同一个类,但是pygame.surface是定义它的模块。

I have seen your code and soure code. 我看过您的代码和原始代码。

surface = pygame.surface ( (400, 400), flags = SRCALPHA, depth = 32)

In your code, "surface" is lowercase, which is a python module, so python interpreter tells you the err msg. 在您的代码中,“ surface”是小写字母,它是一个python模块,因此python解释器会告诉您err msg。

surface = pygame.Surface( (400,400), flags=SRCALPHA, depth=32 )

In source code, "Surface" is capital, which may be a class. 在源代码中,“表面”是大写字母,可以是一个类。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM