python - 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
......
解决方案
模块不是类。如果您的类TextField
在一个名为 的模块中TextField
,那么它被称为TextField.TextField
.
或将您的导入更改为
from TextField import TextField
推荐阅读
- laravel - 在验证之前运行方法之后的 Laravel 验证器
- google-apps-script - 在 Google 电子表格中获取所有开发者元数据
- python - 我正在尝试学习如何从另一个文件(我制作的)导入和使用一个类。为什么我的输出没有显示 7?
- python - 是否可以创建一个要在 android 手机中运行的 python 脚本来自动执行某些任务?
- linux - CentOS7 中的 VPS,在启动我的 vps 时运行 Files JAVA
- apache-pig - 替换空值在猪中不起作用
- excel - 用于 if -then- else 语句的 VBA
- jenkins - Jenkins管道脚本变量被覆盖?
- python - 如何仅从 CSV 文件的树视图中读取新值?
- java - 以编程方式检索 Spring FeignClient 调用的 URL