Reputation: 27
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 no images.
The path "C:\Users\a****\Desktop\arrayofimages.txt" has:
> C:\\Users\\a****\\Pictures\\photoimage1\\1.jpg
> C:\\Users\\a****\\Pictures\\photoimage1\\2.jpg
> C:\\Users\\a****\\Pictures\\photoimage1\\3.jpg
> C:\\Users\\a****\\Pictures\\photoimage1\\4.jpg
> C:\\Users\\a****\\Pictures\\photoimage1\\5.jpg
> C:\\Users\\a****\\Pictures\\photoimage1\\6.jpg
> C:\\Users\\a****\\Pictures\\photoimage1\\7.jpg
The path C:\Users\a****\Desktop\picturematch.txt has:
> cat
> clouds
> water
> stick
> gun
> nails
> shoes
This is the code:
from random import randint
import tkinter as tk
from PIL import Image, ImageTk
import glob
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)
##empty list for image path
listA = []
##empty list for word that matches the image
listB = []
a=0
b=0
i=0
j=0
##this is to make sure that there is no duplicates as it only appends the element that is not on the list.
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 images
def change_image_label(label):
def change_image():
global i
if i!=3:
img = ImageTk.PhotoImage(Image.open(listA[i]))
label.config(image=img)
label.place(x=450,y=250,anchor="center")
i+=1
label.after(4000,change_image)
else:
label.destroy()
change_image()
##label for the word
def change_word_label(label1):
def change_word():
global j
if j!=3:
label1.config(text=(listB[j]))
label1.place(x=450,y=350,anchor="center")
j+=1
label1.after(4000,change_word)
else:
label1.destroy()
change_word()
root = tk.Tk()
root.title("Memory game")
root.geometry("900x500")
label = tk.Label(root)
label.pack()
change_image_label(label)
label1 = tk.Label(root)
label1.pack()
change_word_label(label1)
root.mainloop()
Upvotes: 0
Views: 2287
Reputation: 27
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()
Upvotes: 1