首页 > 解决方案 > Python - 创建一个模块的实例,得到一个错误

问题描述

我正在创建一个通用文本字段,可以在许多 python 海龟项目中使用。我正在尝试创建它的一个实例,但出现此错误:

>>> import TextField
>>> tf = TextField('None', False)
Traceback (most recent call last):
  File "<pyshell#1>", line 1, in <module>
    tf = TextField('None', False)
TypeError: 'module' object is not callable
>>> 

模块中的什么导致这种类型的错误?我完全编写了这个模块,但在创建它的实例时出现错误:( ...我需要在这个模块中做什么才能使其“可调用”?我尝试添加 adef __call__(self):但这不会影响问题全部,也不会产生任何错误。这是最有可能发生问题的脚本的开头:

# Created by SUPERMECHM500 @ repl.it
# Edited by cdlane @ stackoverflow.com

class TextField:
    TextFieldBorderColor = '#0019fc'
    TextFieldBGColor = '#000000'
    TextFieldTextColor = '#ffffff'

    ShiftedDigits = {
                     '1':'!',
                     '2':'@',
                     '3':'#',
                     '4':'$',
                     '5':'%',
                     '6':'^',
                     '7':'&',
                     '8':'*',
                     '9':'(',
                     '0':')'
                    }

    def __init__(self, command, CanBeEmpty): # Ex. textField = TextField('Execute()', True)
        self.CmdOnEnter = command
        self.turtle = Turtle()
        self.CanBeEmpty = CanBeEmpty
        self.turtle.speed('fastest')
        self.inp = []
        self.FullOutput = ""
        self.TextSeparation = 7
        self.s = self.TextSeparation
        self.key_shiftL = False

......

标签: pythonmodule

解决方案


模块不是类。如果您的类TextField在一个名为 的模块中TextField,那么它被称为TextField.TextField.

或将您的导入更改为

from TextField import TextField

推荐阅读