【问题标题】:How do I solve an attribute error?如何解决属性错误?
【发布时间】:2013-11-18 04:16:02
【问题描述】:

就像我之前所说的,我的代码(或我正在处理的当前项目)充满了错误。到目前为止,我至少解决了十几个错误或更多,老实说我只是放弃了。我的意思是上帝知道还有多少。

我目前遇到的问题是 AttributeError,在我看来这是最容易修复的错误之一,但我似乎已经进入了意大利面条模式,我不知道如何解决这个问题。

{错误本身:

Traceback (most recent call last):
      File "C:\Users\Burak\Desktop\boxtrial.py", line 87, in <module>
        myScreen.addPane("1")
      File "C:\Users\Burak\Desktop\boxtrial.py", line 67, in addPane
        myPane.drawPane()
      File "C:\Users\Burak\Desktop\boxtrial.py", line 19, in drawPane
        self.Screen.blit(self.font.render(textToDisplay, True, (black)), (250, 115))
AttributeError: 'Pane' object has no attribute 'Screen'

}

我将在下面列出代码,但我觉得我应该解释我正在尝试做的事情,以便您对代码有一定的了解。 基本上在主循环中,我调用了 "Class Screen",它有助于创建一个运行后出现的 PyGame 屏幕。在该屏幕上,我试图让矩形出现在屏幕上的固定位置(坐标是特定的,但我在代码中使用的坐标仅用于测试目的)。然后我有另一个名为 "Pane" 的类,并且这个类在那里,以便我可以在屏幕内绘制类窗格的许多实例(如果这有意义的话)。

如果有人可以帮助我摆脱错误,那将是非常有帮助的,但是如果您认为这不是解决问题的好方法,那么请成为我的客人提出或教我一个更好的方法做同样的事情的方法。

{代码:

import pygame
import sys
from pygame.locals import *

white = (255,255,255)
black = (0,0,0)
objs = []
MAIN_BUTTON = 1


class Pane():

    def __init__(self, textToDisplay, coordinates, screen):
        self.textToDisplay = textToDisplay
        self.coordinates = coordinates
        self.screen = screen

    def drawPane(self):
        self.Screen.blit(self.font.render(textToDisplay, True, (black)), (250, 115))
        pygame.draw.rect(self.screen, (black), self.coordinates, 2)
        pygame.display.update()


class Screen():

    #constants/array(?) outlining the x,y boundaries of each of x10 panes

    #Note to self - Remember to change co-ordinate values
    NoOfPanes = 0
    Panes = []

    def __init__(self):
        pygame.init()
        pygame.display.set_caption('Box Test')

        self.font = pygame.font.SysFont('Arial', 25)
        Screen = pygame.display.set_mode((1000,600), 0, 32)
        self.screen = Screen
        self.screen.fill((white))

        pygame.display.update()

    def addPane(self, textToDisplay):
        paneLocs = [(175, 75, 200, 100), 
                     (0, 0, 200, 100), 
                     (600, 400, 200, 100), 
                     (175, 75, 200, 100), 
                     (175, 75, 200, 100), 
                     (175, 75, 200, 100), 
                     (175, 75, 200, 100), 
                     (175, 75, 200, 100), 
                     (175, 75, 200, 100), 
                     (175, 75, 200, 100)
                     ]

        if self.NoOfPanes > 10:
            print("Limit Reached")            
        else:            
            myPane = Pane(textToDisplay, paneLocs[self.NoOfPanes], Screen)
            myPane.drawPane()
            self.NoOfPanes = self.NoOfPanes + 1
            pygame.display.update()

    def mousePosition(self):
        global clickPos
        global releasePos
        for event in pygame.event.get():
            if event.type == MAIN_BUTTON:
                self.Pos = pygame.mouse.get_pos()
                return MAIN_BUTTON
            else:
                return False


if __name__ == '__main__':
    myScreen = Screen()
    myScreen.addPane("1")
    myScreen.addPane("2")
    myScreen.addPane("3")
    myScreen.addPane("4")

    while True:
        ev = pygame.event.get()
        for event in ev:
            if event.type == pygame.MOUSEBUTTONUP:
                posx,posy = pygame.mouse.get_pos()
                if (posx >= 175 and posx <= 375) and (posy >= 75 and posy <= 175):
                    print("BOB") #Bob was there just for test purposes

        for event in pygame.event.get():        
            if event.type == pygame.QUIT:
                pygame.quit(); sys.exit();

【问题讨论】:

  • 你在self.Screenself.screen之间切换
  • 另外,您在 Screen __init__ 方法中使用变量 Screen 隐藏类 Screen。您应该选择另一个变量名,或切换为小写。

标签: python pygame attributeerror


【解决方案1】:

解决你的问题。

class Pane():

    def __init__(self, textToDisplay, coordinates, screen):
         ...
        self.screen = screen

    def drawPane(self):
        self.Screen.... # <<< HERE

【讨论】:

    猜你喜欢
    • 2023-04-02
    • 1970-01-01
    • 2019-09-14
    • 2018-08-03
    • 2019-09-08
    • 2020-10-25
    • 2015-07-25
    • 2017-08-13
    • 2021-05-24
    相关资源
    最近更新 更多