簡體   English   中英

類型對象沒有屬性

[英]Type Object has no attribute

我正在開發一個程序,但我收到錯誤“類型對象‘卡片’沒有屬性文件名。我已經尋找了這個問題的答案,但我所看到的沒有一個與此類似。

class Card:
RANKS = (1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13)
SUITS = ('s', 'c','d','h')
BACK_Name = "DECK/b.gif"

def __init__(self, rank, suit):
    """Creates a card with the given rank and suit."""
    self.rank = rank
    self.suit = suit
    self.face = 'down'
    self._fileName = 'DECK/' + str(rank) + suit[0] + '.gif'

class TheGame(Frame):

    def __init__(self):
        Frame.__init__(self)
        self.master.title("Memory Matching Game")
        self.grid()

        self.BackImage = PhotoImage(file = Card.BACK_Name)
        self.cardImage = PhotoImage(file = Card.fileName)

解決這個問題的任何幫助都會很棒。 謝謝。

您具有三個屬性: RANKSSUITSBACK_Name

class Card:
    # Class Attributes:
    RANKS = (1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13)
    SUITS = ('s', 'c','d','h')
    BACK_Name = "DECK/b.gif"

您尚未將fileName定義為類屬性,因此嘗試獲取名為fileNameAttributeError將引發AttributeError指示它不存在。

這是因為fileName ,或者更確切地說, _fileName已通過self._filename定義為實例屬性:

# Instance Attributes:
def __init__(self, rank, suit):
    """Creates a card with the given rank and suit."""
    self.rank = rank
    self.suit = suit
    self.face = 'down'
    self._fileName = 'DECK/' + str(rank) + suit[0] + '.gif'

要訪問此屬性,您必須首先使用c = Card(rank_value, suit_value)創建Card對象的實例 那么你就可以訪問_filename通過c._filename

暫無
暫無

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

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