1

For each winning item i would like to display its corresponding picture on the GUI. At the moment i am writing a bunch of code shown in the if statements to achieve this. I am sure i can use some sort of a loop to reduce the amount of code but unsure at this point how i can achieve this.

I am also having issues with removing the image after each non-winning roll. My current attempt at solving this is shown in the code provided within the else statement at the bottom.

chance_of_drop = random.randint(1,100)
if chance >= chance_of_drop:
    winner = np.random.choice(Items, p=probabilities)
    drop['text'] = "You've recieved a drop: " + winner

    if winner == Items[0]:
        Loot_IMG = PhotoImage(file=Images[0])
        reward_img = Label(GUI, image = Loot_IMG, background = bg_color)
        reward_img.Loot_IMG = Loot_IMG #
        reward_img.grid(row = 3, column=1, sticky = N)
    elif winner == Items[1]:
        Loot_IMG = PhotoImage(file=Images[1])
        reward_img = Label(GUI, image = Loot_IMG, background = bg_color)
        reward_img.Loot_IMG = Loot_IMG #
        reward_img.grid(row = 3, column=1, sticky = N)
    elif winner == Items[2]:
        Loot_IMG = PhotoImage(file=Images[2])
        reward_img = Label(GUI, image = Loot_IMG, background = bg_color)
        reward_img.Loot_IMG = Loot_IMG #
        reward_img.grid(row = 3, column=1, sticky = N)
   # AND SO ON.....


#print("You've recieved a drop:", winner)

else:
    luck['text'] = "You are unlucky"

    #REMOVING IMAGE DOES NOT WORK
    Loot_IMG = PhotoImage(file="")
    reward_img = Label(GUI, image = Loot_IMG, background = bg_color)
    reward_img.Loot_IMG = Loot_IMG #
    reward_img.grid(row = 3, column=1, sticky = N)

ALSO i will provide both of the lists i have constructed further up within the function:

    Images = ["loot/Dexterous_prayer_scroll.png", "loot/Arcane_prayer_scroll.png",
      "loot/Twisted_buckler.png", "loot/Dragon_hunter_crossbow.png",
      "loot/Dinh's_bulwark.png", "loot/Ancestral_hat.png", "loot/Ancestral_robe_top.png",
      "loot/Ancestral_robe_bottom.png", "loot/Dragon_claws.png", "loot/Elder_maul.png",
      "loot/Kodai_insignia.png", "loot/Twisted_bow.png"]


# indivdual drop rates
Items = ["Dexterous prayer scroll", "Arcane prayer scroll",
         "Twisted buckler", "Dragon hunter crossbow",
         "Dinh's bulwark", "Ancestral hat", "Ancestral robe top",
         "Ancestral robe bottom", "Dragon claws",
         "Elder maul", "Kodai insignia", "Twisted bow"]
Tomble
  • 53
  • 2
  • 12

1 Answers1

0

You can factor out the duplicated code by using the built-in zip() function and something along these lines:

if chance >= chance_of_drop:
    winner = np.random.choice(Items, p=probabilities)
    drop['text'] = "You've recieved a drop: " + winner

    for item, image in zip(Items, Images):
        if winner == item:
            Loot_IMG = PhotoImage(file=image)
            reward_img = Label(GUI, image=Loot_IMG, background=bg_color)
            reward_img.Loot_IMG = Loot_IMG #
            reward_img.grid(row=3, column=1, sticky=N)
            break
else:
    try:
        reward_img.destroy()  # Remove any existing Label.
    except NameError:
        pass  # Doesn't exist, ignore.

Also note that the tkinter PhotoImage class only supports images in .gif, .pgm, or .ppm format. To load .png images you will need to use the Python Imaging Library (PIL) submodule's ImageTk.PhotoImage class instead.

Update to note:

Tk 8.6 added built-in support for the .png image file format, so you may not need to use PIL.

martineau
  • 119,623
  • 25
  • 170
  • 301
  • Thanks! Any ideas on how i can remove the images lets say when a button is pressed? Also the images seem to be stacking on top of each other after every roll. Unsure if i have to remove the image at the start of the function aswell – Tomble Mar 22 '19 at 17:21
  • Yes, try `reward_img.destroy()` (assuming `reward_img` was previously defined). – martineau Mar 22 '19 at 17:23
  • Okay, that didnt work. reward_img is defined after the function - should i change the layout so that the function is underneath my tkinter code? UnboundLocalError: local variable 'reward_img' referenced before assignment – Tomble Mar 22 '19 at 17:26
  • Maybe. There isn't enough information in your question for me answer definitely, but however you do it, you'll need to keep track of the current `reward_img` `Label` widget if you want to delete it at some point. Suggest you see accepted answer to question [Best way to structure a tkinter application](https://stackoverflow.com/questions/17466561/best-way-to-structure-a-tkinter-application). – martineau Mar 22 '19 at 17:31
  • The loop solved my first issue, still trying to solve the image deleting issue. Marked as solved - thanks – Tomble Mar 22 '19 at 18:02