我遇到的問題是相同的圖像(標籤中)除最後一個標籤外不會顯示。這是一張照片,向你展示我的意思。在這張圖片中,我生成了7張卡片,然後生成了2.我希望空白處填充問號。 Tkinter具有相同圖像的圖像標籤除最後一個外不可見
可以創建的最大卡數範圍爲0-8。如果我生成6張卡片然後生成4張卡片,我希望卡片5-6重新回到隨機卡片圖像。
卡類
import tkinter as tk
from PIL import Image, ImageTk
class Card:
def __init__(self, name, imageURL, cardType, rarity):
self.name = name
self.imageURL = imageURL
self.cardType = cardType
self.rarity = rarity
def __repr__(self):
print(self.name)
def __str__(self):
return self.name
def returnImage(self):
self.timage = Image.open(self.imageURL)
if not self.timage.size == (212,263):
self.timage = self.timage.resize((212,263), Image.ANTIALIAS)
self.tphoto = ImageTk.PhotoImage(self.timage)
return self.tphoto
def createLabel(self, parent):
self.timage = Image.open(self.imageURL)
if not self.timage.size == (212,263):
self.timage = self.timage.resize((212,263), Image.ANTIALIAS)
self.tphoto = ImageTk.PhotoImage(self.timage)
self.cardLabel = tk.Label(parent, image = self.tphoto)
self.cardLabel.image = self.tphoto
return self.cardLabel
主要
randomUnknown = Card("RANDOM", PATH + '\\random-unknown.png', 'N/A', 'N/A')
#Note: randomUnknown is in a separate file. The card.randomUnknown just calls it
randomCard = card.randomUnknown
card1 = randomCard
card2 = randomCard
card3 = randomCard
card4 = randomCard
card5 = randomCard
card6 = randomCard
card7 = randomCard
card8 = randomCard
card1Label = card1.createLabel(root)
card2Label = card2.createLabel(root)
card3Label = card3.createLabel(root)
card4Label = card4.createLabel(root)
card5Label = card5.createLabel(root)
card6Label = card6.createLabel(root)
card7Label = card7.createLabel(root)
card8Label = card8.createLabel(root)
這是在我的代碼的頂部創建窗口後。每個標籤都與我想要顯示的圖像正確顯示。像這樣在這裏:
MY未遂
每個按鈕被按下時,卡被採摘並顯示。此代碼也運行。我也嘗試將標籤設置爲randomCard.createLabel(root),但同樣的事情發生了。
def randomizeButton():
global currentCards,card1Label, card2Label,card3Label,card4Label,card5Label,card6Label,card7Label,card8Label
currentCards = maxCards
setMax()
randomizeCards()
print('Current Cards: ' + str(currentCards))
if(maxCards == 0):
card1Label.configure(image = randomCard.returnImage())
card2Label.configure(image = randomCard.returnImage())
card3Label.configure(image = randomCard.returnImage())
card4Label.configure(image = randomCard.returnImage())
card5Label.configure(image = randomCard.returnImage())
card6Label.configure(image = randomCard.returnImage())
card7Label.configure(image = randomCard.returnImage())
card8Label.configure(image = randomCard.returnImage())
if(maxCards == 1):
card2Label.configure(image = randomCard.returnImage())
card3Label.configure(image = randomCard.returnImage())
card4Label.configure(image = randomCard.returnImage())
card5Label.configure(image = randomCard.returnImage())
card6Label.configure(image = randomCard.returnImage())
card7Label.configure(image = randomCard.returnImage())
card8Label.configure(image = randomCard.returnImage())
if(maxCards == 2):
card3Label.configure(image = randomCard.returnImage())
card4Label.configure(image = randomCard.returnImage())
card5Label.configure(image = randomCard.returnImage())
card6Label.configure(image = randomCard.returnImage())
card7Label.configure(image = randomCard.returnImage())
card8Label.configure(image = randomCard.returnImage())
if(maxCards == 3):
card4Label.configure(image = randomCard.returnImage())
card5Label.configure(image = randomCard.returnImage())
card6Label.configure(image = randomCard.returnImage())
card7Label.configure(image = randomCard.returnImage())
card8Label.configure(image = randomCard.returnImage())
if(maxCards == 4):
card5Label.configure(image = randomCard.returnImage())
card6Label.configure(image = randomCard.returnImage())
card7Label.configure(image = randomCard.returnImage())
card8Label.configure(image = randomCard.returnImage())
if(maxCards == 5):
card6Label.configure(image = randomCard.returnImage())
card7Label.configure(image = randomCard.returnImage())
card8Label.configure(image = randomCard.returnImage())
if(maxCards == 6):
card7Label.configure(image = randomCard.returnImage())
card8Label.configure(image = randomCard.returnImage())
if(maxCards == 7):
card8Label.configure(image = randomCard.returnImage())
標籤設置在網格中。
card1Label.grid(row = 0, column = 0)
card2Label.grid(row = 0, column = 1)
card3Label.grid(row = 0, column = 2)
card4Label.grid(row = 0, column = 3)
card5Label.grid(row = 1, column = 0)
card6Label.grid(row = 1, column = 1)
card7Label.grid(row = 1, column = 2)
card8Label.grid(row = 1, column = 3)
如果您需要了解更多信息,我會很樂意提供。我試圖提供所有必要的信息。
你能把整個代碼?我沒有看到多少文件......你能解釋一下你想要的內容嗎? –
@GiovanniGianni我想用'?'標籤在他們出現在頂部圖片的所有空白處。現在,圖像僅出現在最後一個標籤上。整個代碼約700行左右,我相當確定問題在於這些代碼片段。你確定?編輯:我有大約70張圖片 –
你能提供'randomCard.returnImage()'的代碼嗎?我認爲這可能會導致它... – StellarDoor5319