0

I've been programming a random operator name generator for Rainbox Six Siege and I want the operators picture to appear when their name comes up. The image appears fine, but it won't go away. This is my Code:

    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()

Note: This is still a WIP, all I need is to know how to get rid of the pictures once they appear. This is what it looks like when more than one image appears: https://i.stack.imgur.com/Wiqfq.jpg

Aidan Christopher
  • 161
  • 2
  • 2
  • 10

1 Answers1

2

You are adding a new Label every time you call that function. Instead, you should make the Label only once (probably in the initialization stage), and update the picture. Just like you update the text for nameLabel, plus the step to keep the reference.

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")

and your whole code should look like:

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()
Nae
  • 14,209
  • 7
  • 52
  • 79
Novel
  • 13,406
  • 2
  • 25
  • 41
  • photo_label is not defined – Aidan Christopher Oct 24 '17 at 23:58
  • @AidanChristopher I think `photo_label` is called `label` in your own code snippet. – Nae Oct 25 '17 at 00:39
  • @AidanChristopher I think this approach is way better. It is basically what you're doing with the `nameLabel` as well so it's more consistent. Just add `photo_label = tkinter.Label()` before `def pickName():` of this answer. – Nae Oct 25 '17 at 03:31
  • @Nae When i use this code, no image shows up at all when i click "Pick Attack" – Aidan Christopher Oct 25 '17 at 03:47
  • @AidanChristopher Well then update your question to reflect your exact situation of that. Because it works for me and I don't know what can be causing it. But this DOES work for me. – Nae Oct 25 '17 at 13:28
  • @AidanChristopher You're right this answer is missing `pack` method. I will try to edit the answer. – Nae Oct 25 '17 at 15:06
  • @Nae just wrote photo_label.pack() after everything and worked perfectly. Thank you for all the help you've done it's made a big difference – Aidan Christopher Oct 25 '17 at 15:30