0

I tried making a code that displays images in a tkinter window along with a text underneath however its positioned in the top but I want it in the center.

The path "C:\Users\ariel\Desktop\arrayofimages.txt" has:

C:\Users\ariel\Pictures\photoimage1\1.jpg
C:\Users\ariel\Pictures\photoimage1\2.jpg
C:\Users\ariel\Pictures\photoimage1\3.jpg
C:\Users\ariel\Pictures\photoimage1\4.jpg
C:\Users\ariel\Pictures\photoimage1\5.jpg
C:\Users\ariel\Pictures\photoimage1\6.jpg
C:\Users\ariel\Pictures\photoimage1\7.jpg

The path C:\Users\ariel\Desktop\picturematch.txt has:

cat
clouds
water
stick
gun
nails
shoes
from random import randint
import tkinter as tk
from PIL import Image, ImageTk
import glob

a=0
i=0
j=0
k=0

image_list = []
with open("C:\\Users\\ariel\\Desktop\\arrayofimages.txt") as file:
    for line in file:
        line6 = line.strip()
        image_list.append(line6)

Positionimage1 = []
with open("C:\\Users\\ariel\\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, compound=tk.CENTER)
            label1.image = img2
            label1.pack()
            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], compound=tk.CENTER)
            label2.pack()
            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()

Edited by Mae_3

2
Contributors
1
Reply
21
Views
5 Months
Discussion Span
Last Post by rproffitt
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.