在Python3中将元素传递给多个函数
因此,我正在开发一个程序,在一副牌中洗牌,并在用户点击“交易”按钮时返回一张牌的图片。但是我试图将在Python3中将元素传递给多个函数,python,python-3.x,Python,Python 3.x,因此,我正在开发一个程序,在一副牌中洗牌,并在用户点击“交易”按钮时返回一张牌的图片。但是我试图将fileName从deal函数传递到refreshImages函数,我似乎无法让它工作。想知道是否有人能告诉我怎么做,并解释为什么。谢谢 def deal(self): card = self.deck.deal() self.stateLabel["text"] = str(card) fileName = "DECK/" + str(card.rank) + card.s
fileName
从deal
函数传递到refreshImages
函数,我似乎无法让它工作。想知道是否有人能告诉我怎么做,并解释为什么。谢谢
def deal(self):
card = self.deck.deal()
self.stateLabel["text"] = str(card)
fileName = "DECK/" + str(card.rank) + card.suit[0] + ".gif"
self.refreshImages(fileName)
if len(self.deck) == 0:
self.dealBtn["state"] = "disabled"
def shuffle(self):
card = self.deck.shuffle()
fileName = "DECK/" + str(card.rank) + card.suit[0] + "gif"
self.refreshImages(fileName)
if len(seld.deck) == 0:
self.dealBtn["state"] = "disabled"
def refreshImages(self):
"""Updates the images in the window."""
self.image = PhotoImage(file = fileName)
self.cardLabel1["image"] = self.image
由于您的
deal
方法使用文件名作为参数调用refreshImages
方法,因此refreshImages
方法应使用此类参数声明;否则调用方的局部变量fileName
将不会神奇地传递给被调用的方法:
def refreshImages(self, fileName):
"""Updates the images in the window."""
self.image = PhotoImage(file = fileName)
self.cardLabel1["image"] = self.image
我一开始是这么做的,但我猜我输入错了文件名。。。我是个白痴。不过非常感谢。