如何解决属性错误? [英] How do I solve an attribute error?

查看:134
本文介绍了如何解决属性错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

So like I said before my code (Or my current project that I am working on) is riddled with errors. So far I have at least solved a dozen errors or more and honestly I just give up. I mean God knows how many more there are.

我当前遇到的问题是AttributeError,我认为这是最容易修复的错误之一,但是我似乎已经完成了意大利面条模式,并且不知道如何解决该问题.

The current problem that I am having is an AttributeError which is in my opinion one of the easiest errors to fix however I seem to have gone in to complete spaghetti mode and I have no clue on how to fix the problem.

{错误本身:

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'

}

我将在下面列出代码,但我觉得我应该解释我要做什么,因此您对代码有某种了解. 基本上在主循环中,我调用类屏幕" ,这有助于创建一个PyGame屏幕,该屏幕一旦运行即可显示.在该屏幕上,我试图使矩形以固定位置显示在屏幕上(坐标是特定的,但我在代码中使用的只是用于测试目的).然后,我有另一个名为"Pane" 的类,并且该类在那里,因此我可以在屏幕内绘制该类窗格的许多实例(如果这样的话).

I will list the code below but I feel as if I should explain what I am trying to do so you have some sort of understanding of the code. Basically in the main loop I call upon the "Class Screen" which helps to create a PyGame screen that comes up once run. On that screen I am trying to get rectangles to appear on the screen in fixed positions (The coordinates are specific but the ones I use on the code are just for test purposes). I then have another class that is called "Pane" and this class is there so that I can draw many instances of the class pane within screen (If that makes sense).

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

If someone can help me get rid of the error that would be of grate help, but if you think that this is not a good way of solving the problem then please be my guest to come up with or teach me of a better way to do the same thing.

{代码:

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();

推荐答案

解决您的问题.

class Pane():

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

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

这篇关于如何解决属性错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆