Python Tkinter: How To Display An Array Of Images In A Loop?
I tried making a code that displays images in a tkinter window along with a text underneath however there's no images displayed when I run the code. The word shows up but there's n
Solution 1:
This works now.
from random import randint
import tkinter as tk
from PIL import Image, ImageTk
a=0
i=0
j=0
k=0
image_list = []
with open("C:\\Users\\a****\\Desktop\\arrayofimages.txt") as file:
for line in file:
line6 = line.strip()
image_list.append(line6)
Positionimage1 = []
with open("C:\\Users\\a****\\Desktop\\picturematch.txt") as file:
for line in file:
line7 = line.strip()
Positionimage1.append(line7)
listA = []
listB = []
while a<3:
randomimages = randint(0,6)
randomimages1 = image_list[randomimages]
randomword = Positionimage1[randomimages]
if randomimages1 not in listA:
listA.append(randomimages1)
listB.append(randomword)
a+=1
else:
pass
##label for the word
def change_picture_label(label1):
def change_word():
global j
if j!=3:
img2 = ImageTk.PhotoImage(Image.open(listA[j]).resize((200, 250)))
label1.configure(image = img2)
label1.image = img2
label1.place(x=450,y=250,anchor="center")
j+=1
label1.after(4000,change_word)
else:
label1.destroy()
change_word()
def change_word_label(label1):
def change_text():
global k
if k!=3:
label2.configure(text=listB[k])
label1.place(x=450,y=390,anchor="center")
k+=1
label2.after(4000,change_text)
else:
label2.destroy()
change_text()
root = tk.Tk()
root.title("Memory game")
root.geometry("900x500")
label1 = tk.Label(root)
change_picture_label(label1)
label2 = tk.Label(root)
change_word_label(label2)
root.mainloop()
Post a Comment for "Python Tkinter: How To Display An Array Of Images In A Loop?"