【问题标题】:How to make image disappear in tkinter如何使图像在 tkinter 中消失
【发布时间】:2017-10-24 23:46:42
【问题描述】:

我一直在为《彩虹六号:围攻》编写一个随机操作员姓名生成器,我希望在他们的名字出现时出现操作员图片。图像看起来很好,但它不会消失。这是我的代码:

    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

【问题讨论】:

  • 我看不到任何地方你甚至试图让旧的小部件消失。你不会破坏它,也不会隐藏它。
  • @BryanOakley 我认为他不知道该怎么做。
  • @BryanOakley 是的,我不知道如何销毁它,这是我的问题
  • @Nae 可能,但我对如何在我的程序中实现它太缺乏经验

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


【解决方案1】:

每次调用该函数时,您都会添加一个新标签。相反,您应该只制作一次标签(可能在初始化阶段),然后更新图片。就像您更新 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()

【讨论】:

  • photo_label 未定义
  • @AidanChristopher 我认为 photo_label 在您自己的代码 sn-p 中称为 label
  • @AidanChristopher 我认为这种方法要好得多。这基本上也是您对nameLabel 所做的事情,因此它更加一致。只需在此答案的def pickName(): 之前添加photo_label = tkinter.Label()
  • @Nae 当我使用这个代码时,当我点击“Pick Attack”时根本没有图像显示
  • @AidanChristopher 那么更新您的问题以反映您的确切情况。因为它对我有用,我不知道是什么原因造成的。但这对我有用。
猜你喜欢
  • 2021-04-25
  • 1970-01-01
  • 1970-01-01
  • 2012-08-30
  • 1970-01-01
  • 2020-04-19
  • 2020-11-30
  • 2015-05-25
相关资源
最近更新 更多