我正在编写的这段代码应该在每次点击一个按钮时添加一张图片,但是在我做了多个之后,最后一张图片就会消失。
{{1}}
我该如何解决这个问题?
答案 0 :(得分:0)
问题是,当您再次拨打deal
时,PhotoImage
中存储的card
会被另一个import tkinter
suits = ["club", "heart", "diamond", "spade"]
faces = ["jack", "queen", "king"]
pictures = []
def deal():
global value
global card
global deck
value, card = deck.pop(0)
print(deck)
return card
def image():
global count
tkinter.Label(root, image=deal()).grid(row=1, column=count)
count += 1
root = tkinter.Tk()
deck = []
for x in range(1, 11):
for y in suits:
pic = "cards/{}_{}.png".format(x, y)
img = tkinter.PhotoImage(file=pic)
pictures.append(img)
deck.append((x, img))
for z in faces:
pic = "cards/{}_{}.png".format(z, y)
img = tkinter.PhotoImage(file=pic)
pictures.append(img)
deck.append((10, img))
value, card = deck.pop(0)
count = 0
tkinter.Button(root, text="Click me", command=image).grid(row=0, column=0)
root.mainloop()
替换,因此它会被垃圾收集并消失。为防止这种情况,您可以创建图片列表:
.dropDown + .dropDown-content:hover