我正在尝试在Processing.py 中名为Rockets 的选项卡中创建一个名为Rocket 的类。无论我如何导入选项卡(import Rockets
、from Rockets import *
、import Rockets as R
),我都会得到:
AttributeError: 'module' object has no attribute 'Rocket'.
我尝试将类定义放在同一个选项卡中,它工作正常,所以我认为这是一个导入问题,但我找不到我的错误。
主选项卡:
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()
火箭
选项卡
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 )。
火箭类(对象):
火箭类:
还有我们的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/