我正在尝试在Processing.py中名为Rockets的选项卡中创建一个名为Rocket的类。无论我如何导入选项卡(import Rockets
,from Rockets import *
,import Rockets as R
),我都会得到:
AttributeError:“模块”对象没有属性“火箭”。
我尝试将类定义放在同一选项卡中,并且工作正常,因此我认为这是导入问题,但我找不到我的错误。
主标签:
import Rockets
w_width = 800
w_height = 900
r1 = Rocket(w_width/2, w_height-30)
def setup ():
size(w_width, w_height)
background(127)
def draw ():
background(127)
r1.show()
Rockets
标签class Rocket(object): #I'm not sure if i must put (object) or not, just saw that in tutorials
def __init__(self, x, y):
self.x = x
self.y = y
self.hgt = 30
self.wdt = 10
def show (self):
rectMode(CENTER)
stroke(255)
strokeWeight(2)
fill(0, 127)
rect(self.x, self.y, self.wdt, self.hgt)
最佳答案
在类声明中跳过基类(object)
。目前,Rocket
尚未从任何其他对象继承(请参见Inheritance)。class Rocket(object):
class Rocket:
而我们Rockets
(模块)名称空间:
import Rockets
w_width = 800
w_height = 900
r1 = Rockets.Rocket(w_width/2, w_height-30)
或使用import-from语句(请参见Importing * From a Package):
from Rockets import *
w_width = 800
w_height = 900
r1 = Rocket(w_width/2, w_height-30)
关于python - 无法访问Processing.py中其他选项卡中的类,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/55750417/