samedi 23 septembre 2017

Cannot display a random image file

I am trying to write some code that will play a sound, ask the user to identify the correct sound, and then show the image if the user is either correct or wrong. However, when I do so I get this error:

Traceback (most recent call last): File "C:\LearnArabic\Program\LearnArabicprogram2.4.1.py", line 42, in 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

The program currently opens a blank tk window but as the error suggests, no image is displayed. I've tried using png files and gif files but I get the same error. Why doesn't it recognize my file path?

Here is my code:

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)




Aucun commentaire:

Enregistrer un commentaire