这是我试图显示图像的代码。 嗯..首先应该在屏幕上显示4张卡片;但是,只有2个。 我不知道为什么会这样。我真的需要帮助。
def __init__(self):
Frame.__init__(self)
self.master.title("Black Jack")
blackjack = Blackjack()
self.grid()
self._playerCardImage = PhotoImage(file=str(blackjack._player._cards[0].fileName))
self._playerCardLabel = Label(self, image = self._playerCardImage)
self._playerCardLabel.grid(row=0, column=0)
self._playerCardImage1 = PhotoImage(file=str(blackjack._player._cards[1].fileName))
self._playerCardLabel1 = Label(self, image = self._playerCardImage1)
self._playerCardLabel1.grid(row=0, column=1)
self._dealerCardImage = PhotoImage(file=str(blackjack._dealer._cards[0].fileName))
self._dealerCardImage = Label(self, image = self._dealerCardImage)
self._dealerCardImage.grid(row=0, column=2)
self._dealerCardImage1 = PhotoImage(file=str(blackjack._dealer._cards[1].fileName))
self._dealerCardImage1 = Label(self, image = self._dealerCardImage1)
self._dealerCardImage1.grid(row=0, column=3)
答案 0 :(得分:4)
前两张卡片的前两行代码:
self._playerCardImage = PhotoImage(file=str(blackjack._player._cards[0].fileName))
self._playerCardLabel = Label(self, image = self._playerCardImage)
self._playerCardLabel.grid(row=0, column=0)
最后2张代码的最后2行代码:
self._dealerCardImage1 = PhotoImage(file=str(blackjack._dealer._cards[1].fileName))
self._dealerCardImage1 = Label(self, image = self._dealerCardImage1)
self._dealerCardImage1.grid(row=0, column=3)
您正在重新初始化_dealerCardImage1
作为标签
你需要将alst 2行改为:
self._dealerCardImage1 = PhotoImage(file=str(blackjack._dealer._cards[1].fileName))
self._dealerCardLabel1 = Label(self, image = self._dealerCardImage1)
self._dealerCardLabel1.grid(row=0, column=3)
答案 1 :(得分:1)
在另一个答案的问题中,您询问是否可以循环创建图像。没有什么可以阻止你做那件事。
例如,如果您想创建4张牌手,则可以执行以下操作:
def __init__(self):
Frame.__init__(self)
self.master.title("Black Jack")
blackjack = Blackjack()
self.grid()
self._images = []
self._labels = []
for column in range(4):
image = PhotoImage(file=str(blackjack._player._cards[column].fileName))
label = Label(self, image = image)
label.grid(row=0, column=column)
self._images.append(image)
self._labels.append(label)