无法访问Processing.py中其他选项卡中的类

时间:2019-04-18 16:40:39

标签: python python-3.x class tabs processing

我正在尝试在Processing.py中名为Rockets的选项卡中创建名为Rocket的类。无论我如何导入选项卡(import Rocketsfrom 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)

1 个答案:

答案 0 :(得分:0)

在类声明中跳过基类(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)