如何使图像在tkinter中消失

时间:2017-10-24 23:46:42

标签: python python-3.x tkinter tk python-3.6

我一直在为Rainbox Six Siege编写一个随机运算符名称生成器,我希望在名字出现时显示运算符图片。图像显得很好,但它不会消失。这是我的代码:

    from tkinter import *
    import tkinter
    import random

    names = ['Sledge','Thatcher','Ash','Thermite','Twitch','Montagne','Glaz','Fuze','Blitz','IQ','Buck','Blackbeard','Capitão','Hibana']
    name = ["Smoke","Mute","Castle","Pulse","Doc","Rook","Kapkan","Tachanka","Jäger","Bandit","Frost","Valkyrie","Caveira","Echo"]
    root = tkinter.Tk()
    def pickName():
        rad = random.choice(names)
        photo = PhotoImage(file=rad+".png")
        label = Label(image=photo)
        label.image = photo  # keep a reference!
        label.pack()
        nameLabel.configure(text=rad, foreground="white", background="blue")
        root.configure(background='blue')
    def pickName1():        nameLabel.configure(text=random.choice(name),background="orange",foreground="black")
        root.configure(background='orange')



    root.title("Operator Picker")

    root.geometry("250x100")

    nameLabel = tkinter.Label(root, text="", font=('Helvetica', 32))
    nameLabel.pack()
    Grid()

    f1 = tkinter.Frame(root, height=100, width=100) #defines frame size in 
    pixels
    f1.pack(side=tkinter.LEFT) #packs on the left
    f1.pack_propagate(0) #tells frame not to let children control size
    pickButton1 = tkinter.Button(f1, command=pickName, text="Pick                         
    Attack",background="blue",foreground="white")
    pickButton1.pack(fill=tkinter.BOTH, expand=1) #takes up all available space

    f2 = tkinter.Frame(root, height=100, width=100)
    f2.pack(side=tkinter.RIGHT)
    f2.pack_propagate(0)
    pickButton2 = tkinter.Button(f2, command=pickName1, text="Pick 
    Defend",background="orange",foreground="black")
    pickButton2.pack(fill=tkinter.BOTH, expand=1)

    root.mainloop()

注意:这仍然是一个WIP,我需要的是知道如何摆脱它们出现的图片。当出现多个图像时,这就是它的样子:https://imgur.com/eroXLLn

1 个答案:

答案 0 :(得分:2)

每次调用该功能时都会添加一个新标签。相反,您应该只使标签一次(可能在初始化阶段),并更新图片。就像更新nameLabel的文本一样,加上保留引用的步骤。

photo_label = tkinter.Label()
def pickName():
    rad = random.choice(names)
    photo = PhotoImage(file=rad+".png")
    photo_label.configure(image = photo)
    photo_label.image = photo  # keep a reference!
    photo_label.pack()

    nameLabel.configure(text=rad, foreground="white", background="blue")

并且您的整个代码应如下所示:

from tkinter import *
import tkinter
import random

names = ['Sledge','Thatcher','Ash','Thermite','Twitch','Montagne','Glaz','Fuze','Blitz','IQ','Buck','Blackbeard','Capitão','Hibana']
name = ["Smoke","Mute","Castle","Pulse","Doc","Rook","Kapkan","Tachanka","Jäger","Bandit","Frost","Valkyrie","Caveira","Echo"]
root = tkinter.Tk()
photo_label = tkinter.Label()
def pickName():
    rad = random.choice(names)
    photo = PhotoImage(file=rad+".png")
    photo_label.configure(image = photo)
    photo_label.image = photo  # keep a reference!
    photo_label.pack()

    nameLabel.configure(text=rad, foreground="white", background="blue")
    root.configure(background='blue')
def pickName1():        nameLabel.configure(text=random.choice(name),background="orange",foreground="black")
root.configure(background='orange')



root.title("Operator Picker")

root.geometry("250x100")

nameLabel = tkinter.Label(root, text="", font=('Helvetica', 32))
nameLabel.pack()
Grid()

f1 = tkinter.Frame(root, height=100, width=100) #defines frame size inpixels
f1.pack(side=tkinter.LEFT) #packs on the left
f1.pack_propagate(0) #tells frame not to let children control size
pickButton1 = tkinter.Button(f1, command=pickName, text="PickAttack",background="blue",foreground="white")
pickButton1.pack(fill=tkinter.BOTH, expand=1) #takes up all available space

f2 = tkinter.Frame(root, height=100, width=100)
f2.pack(side=tkinter.RIGHT)
f2.pack_propagate(0)
pickButton2 = tkinter.Button(f2, command=pickName1, text="PickDefend",background="orange",foreground="black")
pickButton2.pack(fill=tkinter.BOTH, expand=1)

root.mainloop()