我目前正在使用 tkinter 来进行扑克模拟。我试图给每只手(由卡片按钮代表)一个文本来代表他们有多少钱。以下是发牌和返还资金的代码。
#This deals the card to each of the players
def dealCard():
'''Deals the Card to Each of the players'''
global handCount, cards, cardSuit, cardRank, hand
for i in range(amountOfPlayers):
hand = []
while handCount < 2:
card = changeCardHand()
cardSuit = str(card.suit)
cardRank = str(card.rank)
cardImage = showCard(cardSuit, cardRank, width= 75, height = 100)
image_listHand.append(cardImage)
backImage = Image.open(f"PNG-cards-1.3/card_back_black.png")
backImage = backImage.resize((75, 100))
backImage = ImageTk.PhotoImage(backImage)
back_imageList.append(backImage)
cardButton = tkinter.Button(canvas, image = backImage, text = "0", compound = "bottom", command = lambda index = image_listHand.index(cardImage): [flipImage(index)])
cardButton.config(text = lambda index = i: [getPlayerMoney(index)])
button_list.append(cardButton)
cardButton.place(x = (-25 * amountOfPlayers) + (50*(2*i+1) + 120*(i + 1)) + (handCount + 1)*80, y = (300))
handCount = handCount + 1
hand.append(card)
cards.append(hand)
handCount = 0
这是退款代码
def getPlayerMoney(index):
return str(money[index])
这是使用 Entry 来设置每个人开始的金额的代码。
def loadMoney():
'''Loads the money into each of the players hands'''
global potValue
potValue = int(potEntry.get())
for i in range(amountOfPlayers):
money.append(potValue)
potEntry.destroy()
但是,每张卡片的文本不是显示货币价值,而是 lambda 字符串
如何解决这个问题?
现在
text
是一个 lambda 函数。当您打印 lambda 函数时,它也会显示 lambda 字符串。
如果不需要,可以删除 lambda 函数。
cardButton.config(text = [getPlayerMoney(i)])
或者如果您需要 lambda 函数
func = lambda index = i: [getPlayerMoney(index)]
cardButton.config(text = func())