简体   繁体   English

类型对象没有属性

[英]Type Object has no attribute

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 = 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)

Any help to solving this would be great.解决这个问题的任何帮助都会很棒。 thanks.谢谢。

You have three class attributes: RANKS , SUITS and BACK_Name .您具有三个属性: 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"

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.您尚未将fileName定义为类属性,因此尝试获取名为fileNameAttributeError将引发AttributeError指示它不存在。

This is because fileName , or rather, _fileName has been defined as an instance attribute via self._filename :这是因为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'

To access this attribute you must first create an instance of the Card object with with c = Card(rank_value, suit_value) ;要访问此属性,您必须首先使用c = Card(rank_value, suit_value)创建Card对象的实例 then you can access the _filename via c._filename .那么你就可以访问_filename通过c._filename

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM