So I'm writing basic stuff in python with tkinter and PIL and the problem here is that when I run the program, only the last button show the image of the item I asked him to show.
So my program was expected to extract name of items from lines containing the name of a game Champion in a txt document.
Each line in the document looks like "ItemName/stat1/stat2/stat3/ChampionName
".
The code was then supposed to create buttons with a picture of the item on it(I made sure to name the pngs and the item name in the .txt the same and to put everything in the same folder) but in the end, only the last button had an image on it.
What I tried :
- I tried to reduce the number of elements in the txt, it didn't work
- I then thought that the problem was that the variable icon being updated, the image shown would disappear. So I tried to make an array but it didn't work either because what I put in isn't an int value, I know it's about the 'i' but I don't know if I can put something else appropriate.
My first attempt :
from PIL import Image, ImageTk
import tkinter as tk
itemwindo = tk.Tk()
itemwindo.title("Items")
data = open("Ressource.txt","r")
for line in data:
if 'Vi' in line:
(a,b,c,d,e) = line.split("/")
icon = ImageTk.PhotoImage(Image.open(a + '.png'))
bt = tk.Button(itemwindo,image=icon)
bt.pack()
itemwindo.mainloop()
And my second with arrays :
data = open("Ressource.txt","r")
imglist = arr.array('i')
for line in data:
if 'Vi' in line:
(a,b,c,d,e) = line.split("/")
icon = ImageTk.PhotoImage(Image.open(a + '.png'))
imglist.extend([icon])
p = len(imglist)
bt = tk.Button(itemwindo,image=imglist[p])
bt.pack()
itemwindo.mainloop()
I would like each button to display the picture of the affiliated item on it.