簡體   English   中英

如何解決屬性錯誤?

[英]How do I solve an attribute error?

因此,就像我在我的代碼(或我正在開發的當前項目)充滿錯誤之前所說的那樣。 到目前為止,我至少已經解決了十二個或更多的錯誤,老實說,我只是放棄了。 我的意思是上帝知道還有多少。

我當前遇到的問題是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'

}

我將在下面列出代碼,但我覺得我應該解釋自己的意圖,以便您對代碼有所了解。 基本上在主循環中,我調用“類屏幕”該類有助於創建一個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();

解決你的情況。

class Pane():

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

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM