I am working on a program, but I am getting the error "Type object 'Card' has no attribute fileName. I've looked for answers to this, but none that I've seen is in a similar case to this.
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 = rankself.suit = suitself.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)
Any help to solving this would be great. thanks.
You have three class attributes: RANKS
, SUITS
and 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"
You haven't defined fileName
as a class attribute so trying to get an attribute named fileName
will raise an AttributeError
indicating that it doesn't exist.
This is because fileName
, or rather, _fileName
has been defined as an instance attribute via self._filename
:
# Instance Attributes:
def __init__(self, rank, suit):"""Creates a card with the given rank and suit."""self.rank = rankself.suit = suitself.face = 'down'self._fileName = 'DECK/' + str(rank) + suit[0] + '.gif'
To access this attribute you must first create an instance of the Card
object with with c = Card(rank_value, suit_value)
; then you can access the _filename
via c._filename
.