我试图写一些代码来播放声音,要求用户识别正确的声音,然后显示图像,如果用户是正确的或错误的。但是,当我这样做时,我会得到以下错误:
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)
我在马蒂诺的帮助下找到了答案。我在使用PhotoImage时遗漏了一行代码。我更新了“def answers”部分中的两个“if”语句,如下所示:
希望这能帮助任何有类似问题的人!你知道吗
相关问题 更多 >
编程相关推荐