[英]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.