[英]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)
解決這個問題的任何幫助都會很棒。 謝謝。
您具有三個類屬性: RANKS
、 SUITS
和BACK_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
定義為類屬性,因此嘗試獲取名為fileName
的AttributeError
將引發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.