如何修复“AttributeError:可交付实例没有属性'pygameWindow'”

2024-06-16 10:39:20 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在创建一个名为DELIVERABLE的类,并希望在新的类DELIVERABLE中使用实例类“pygameWindow”。如何使用实例类而不出错?你知道吗

我试着改变

pygameWindow.Draw_Black_Line(xBase, yBase, xTip, yTip, b)

self.pygameWindow.Draw_Black_Line(xBase, yBase, xTip, yTip, b)

下面是我的代码--

戴尔03.py

import sys

import Leap

from pygameWindow import PYGAME_WINDOW

from Deliverable import DELIVERABLE

import pygame

sys.path.insert(0, '..')

Deliverable = DELIVERABLE()

def Run_Forever():

    pygameWindow = PYGAME_WINDOW()
    run = True
    while run:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                run = False
        pygameWindow.Prepare()
        controller = Leap.Controller()
        frame = controller.frame()
        handlist = frame.hands
        if not handlist:
            print "nothing"
        else:
            Deliverable.Handle_Frame(frame)
        pygameWindow.Reveal()
Run_Forever()

你知道吗可交付成果.py你知道吗

from pygameWindow import PYGAME_WINDOW

import pygame

class DELIVERABLE:

    xMin = -300
    xMax = 300
    yMin = -300
    yMax = 300

    def __int__(self, controller, pygameWindow, x, y, xMin, xMax, yMin, yMax):
        self.controller = controller
        self.pygameWindow = pygameWindow
        self.x = x
        self.y = y
        self.xMin = xMin
        self.xMax = xMax
        self.yMin = yMin
        self.yMax = yMax
        # pass

    def Handle_Frame(self,frame):
        hand = frame.hands[0]
        fingers = hand.fingers
        for finger in fingers:
            self.Handle_Finger(finger)

    def Handle_Finger(self,finger):
        for b in range(0, 4):
            self.Handle_Bone(b, finger)

    def Handle_Bone(self, b, finger):
        bone = finger.bone(b)
        base = bone.prev_joint
        tip = bone.next_joint

        [xBase, yBase] = self.Handle_Vector_From_Leap(base)
        [xTip, yTip] = self.Handle_Vector_From_Leap(tip)

        self.pygameWindow.Draw_Black_Line(xBase, yBase, xTip, yTip, b)

    def Handle_Vector_From_Leap(self, v):
        x = int(v[0])
        y = int(v[2])
        if (x < self.xMin):
            self.xMin = x
        if (x > self.xMax):
            self.xMax = x
        if (y < self.yMin):
            self.yMin = y
        if (y > self.yMax):
            self.yMax = y
        return x, y

你知道吗游戏窗口.py你知道吗

import pygame
from constants import *


class PYGAME_WINDOW:

    def __init__(self):
        pygame.init()
        self.width = pygameWindowWidth
        self.depth = pygameWindowDepth
        self.screen = pygame.display.set_mode((pygameWindowWidth, pygameWindowDepth))
        self.debug = True

    def Prepare(self):
        # pygame.event.get()
        # pass
        self.screen.fill((255, 255, 255))
        # pygame.display.flip()

    def Reveal(self):
        pygame.display.update()

    def Draw_Black_Line(self,xBase,yBase,xTip,yTip,b):
        xBase = self.ScaleX(xBase, -300, 300, 0, pygameWindowWidth)
        xTip = self.ScaleX(xTip, -300, 300, 0, pygameWindowWidth)
        yBase = self.ScaleY(yBase, -300, 300, 0, pygameWindowDepth)
        yTip = self.ScaleY(yTip, -300, 300, 0, pygameWindowDepth)
        # width = 4-b
        pygame.draw.line(self.screen, (0,0,0), [xBase,yBase], [xTip,yTip], 4-b)

    def Draw_Black_Circle(self, x, y):
        x = self.ScaleX(x, -1000, 1000, 0, pygameWindowWidth)
        y = self.ScaleZ(y, 0, 1200, 0, pygameWindowDepth)
        pygame.draw.circle(self.screen, (0, 0, 0), (x, y), 45, 0)

    def ScaleX(self, value, leapMin, leapMax, windowStart, windowEnd):
        if(leapMax == leapMin):
            print "The maximum data collection range for the Leap Motion device is > 2000. You have chosen a range of 0. Division by 0 is not possible."
        else:
            return (int(((value + (leapMax-leapMin)/2) / float(leapMax - leapMin)) * (windowEnd - windowStart)))

    def ScaleY(self, value, leapMin, leapMax, windowStart, windowEnd):
        if(leapMax == leapMin):
            print "The maximum data collection range for the Leap Motion device along the z-axis is > 1000. You have chosen a range of 0. Division by 0 is not possible."
        else:
            return int(((value + (leapMax-leapMin)/2) / float(leapMax - leapMin)) * (windowEnd - windowStart))

    def ScaleZ(self, value, leapMin, leapMax, windowStart, windowEnd):
        if(leapMax == leapMin):
            print "The maximum data collection range for the Leap Motion device along the z-axis is > 1000. You have chosen a range of 0. Division by 0 is not possible."
        else:
            return int((1-1.75*(value / float(leapMax - leapMin))) * (windowEnd - windowStart))

我希望实例没有属性bug可以修复。你知道吗

self.pygameWindow.Draw_Black_Line(xBase, yBase, xTip, yTip, b)

AttributeError: DELIVERABLE instance has no attribute 'pygameWindow'


Tags: importselfifdefrangepygameleaphandle