python - 无法访问Processing.py中不同选项卡中的类

标签 python python-3.x class tabs processing

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

相关文章:

c++ - ArrayList 的大小未知帮助

python - Python 会在完成写入之前打开文件吗?

python - gimp python - 如何更改像素颜色?

python-3.x - 即使空闲没有显示错误,pygame 事件键也没有响应?

python-3.x - 使用 Paramiko 的基本 SSH 连接失败

c++ - 指针:这行代码是什么意思?

python - 使用 gmail 和 python 发送邮件时出现 SMTPAuthenticationError

python - 在 Pygame 中,跨不同的 fps 值标准化游戏速度

python - 查找哪些包支持 Python 3.x 与 2.7.x

java - 为什么我创建的对象保持为 null?