我遇到的问题是,除最终标签外,相同的图像(标签中)不会显示。这是一张图片,向您展示我的意思。在这张图片中,我生成了7张卡片,然后生成了2张。我希望空白的空格用问号填充。
可以创建的最大卡数范围为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)
创建窗口后,这是我代码的顶部。每个标签都正确显示我想要显示的图像。像这样:
每次按下按钮,都会挑选并显示卡片。此代码也运行。我也尝试将标签设置为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)
如果您需要更多信息,我很乐意提供。我试图提供所有必要的信息。
答案 0 :(得分:1)
Tkinter仅允许每个图像实例使用一次。要多次显示同一图像,您需要为每个标签单独加载图像。
例如,如果您尝试显示相同的图像5次,则需要像这样运行代码:
import tkinter
root = tkinter.Tk()
image1 = tkinter.PhotoImage(file=icon.gif)
label1 = tkinter.Label(root, image=image1)
label1.pack()
image2 = tkinter.PhotoImage(file=icon.gif)
label2 = tkinter.Label(root, image=image2)
label2.pack()
image3 = tkinter.PhotoImage(file=icon.gif)
label3 = tkinter.Label(root, image=image3)
label3.pack()
image4 = tkinter.PhotoImage(file=icon.gif)
label4 = tkinter.Label(root, image=image4)
label4.pack()
image5 = tkinter.PhotoImage(file=icon.gif)
label5 = tkinter.Label(root, image=image5)
label5.pack()
显然,这只是一个例子,并没有在代码中使用变量名或类。