本文介绍了Python Tkinter:如何在循环中显示图像数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试编写一个代码,在 tkinter 窗口中显示图像以及下面的文本,但是当我运行代码时没有显示图像.这个词出现了,但没有图像.

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.

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

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

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

The path C:\Users\a****\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

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

推荐答案

现在可以使用了.

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

这篇关于Python Tkinter:如何在循环中显示图像数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 11:26
查看更多