Python - 创建模块的实例,收到错误

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

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

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

模块中的内容会导致此类错误?我完全写了这个模块,我在创建它的实例时遇到错误:( ...在这个模块中我需要什么才能使它'可调用'?我试过添加一个def __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

......
python module
1个回答
0
投票

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

或者将导入更改为

from TextField import TextField
© www.soinside.com 2019 - 2024. All rights reserved.