Python Tkinter:如何在循环中显示图像数组?

时间:2017-04-30 11:41:43

标签: python-3.x tkinter

我尝试制作一个在tkinter窗口中显示图像的代码以及下面的文本,但是当我运行代码时没有显示图像。这个词显示但没有图像。

路径" C:\ Users \ a **** \ Desktop \ arrayofimages.txt"有:

> 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

路径C:\ Users \ a **** \ Desktop \ picturematch.txt具有:

>     cat
>     clouds
>     water
>     stick
>     gun
>     nails
>     shoes

这是代码:

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()

1 个答案:

答案 0 :(得分:1)

现在可以使用了。

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()