无法在tkinter应用程序中显示随机图像文件

时间:2017-09-23 20:30:23

标签: python image random tkinter

我正在尝试编写一些会播放声音的代码,要求用户识别正确的声音,然后在用户正确或错误时显示图像。但是,当我这样做时,我收到此错误:

Traceback (most recent call last):
  File "C:\LearnArabic\Program\LearnArabicprogram2.4.1.py", line 42, in <module>
      answers(question, possAnswers)
    File "C:\LearnArabic\Program\LearnArabicprogram2.4.1.py", line 37, in answers
        label = Label(master, image=IL, anchor = E)
     File "C:\Program Files (x86)\Python36-32\lib\tkinter\__init__.py", line 2760, in __init__
          Widget.__init__(self, master, 'label', cnf, kw)
       File "C:\Program Files (x86)\Python36-32\lib\tkinter\__init__.py", line 2293, in __init__
          (widgetName, self._w) + extra + self._options(cnf))
       _tkinter.TclError: image "C:\LearnArabic\alphabet\Image\Dal.png" doesn't exist

程序当前打开一个空白的tk窗口,但正如错误所示,没有显示图像。我尝试过使用png文件和gif文件但是我得到了同样的错误。为什么它不能识别我的文件路径?

这是我的代码:

import os, random
from random import choice
import winsound
import time
from tkinter import *

master = Tk()

question = "What is the correct Arabic letter?"

Imagepath = "C:\\LearnArabic\\alphabet\\Image\\"
Soundpath = "C:\\LearnArabic\\alphabet\\Sound\\"
Letter = random.choice(os.listdir(Soundpath))
winsound.PlaySound((Soundpath)+ (Letter), winsound.SND_FILENAME)

def answers(question, possAnswers):
    print(question)
    answerList = ["answer1", "answer2", "answer3", "correct"]
    random.shuffle(answerList)

    for i,j in enumerate(answerList):
        print("%s: %s" %(i, possAnswers[j]))

    inp = int(input(">>> "))

    if answerList[inp] == "correct":
        print("Correct!")
        IL = (Imagepath + (Letter.rsplit(".", 1)[0])+ ".png")
        label = Label(master, image=IL, anchor = E)
        label.image = (IL)
        label.grid(row = 2, column = 0, sticky = S)

    if answerList[inp] != "correct":
        print("Try again fool")
        print("The corect answer was: ", Letter)
        IL = (Imagepath + (Letter.rsplit(".", 1)[0])+ ".png")
        label = Label(master, image=IL, anchor = E)
        label.image = (IL)
        label.grid(row = 2, column = 0, sticky = S)

possAnswers = {"answer1" : random.choice(os.listdir(Soundpath)),
               "answer2" : random.choice(os.listdir(Soundpath)),
               "answer3" : random.choice(os.listdir(Soundpath)),
               "correct" : Letter}
answers(question, possAnswers)

1 个答案:

答案 0 :(得分:0)

我能在马蒂诺的帮助下找到答案。我使用PhotoImage错过了一行代码。我更新了“def answers”部分中的两个“if”语句,如下所示:

    if answerList[inp] == "correct":
        print("Correct!")
        IL = (Imagepath + (Letter.rsplit(".", 1)[0])+ ".png")
        Displayimage = PhotoImage(file=IL)
        label = Label(master, image=Displayimage, anchor = E)
        label.image = (Displayimage)            
        label.grid(row = 2, column = 0, sticky = S)


    if answerList[inp] != "correct":
        print("Try again fool")
        print("The corect answer was: ", Letter)
        IL = (Imagepath + (Letter.rsplit(".", 1)[0])+ ".png")
        Displayimage = PhotoImage(file=IL)
        label = Label(master, image=Displayimage, anchor = E)
        label.image = (Displayimage)            
        label.grid(row = 2, column = 0, sticky = S)

希望这可以帮助任何有类似问题的人!