Python中Playsound模块未指定文件报错的解决求助
解决取消文件选择后触发PlaysoundException的问题
问题描述
点击上传按钮后,在askopenfilename对话框中未选择文件直接关闭(点击叉号),会触发PlaysoundException异常,尝试使用try/except语句捕获该异常但未成功,寻求预防或解决此问题的方法。
报错信息(翻译后)
回溯(最近的调用最后): 文件 "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.10_3.10.2288.0_x64__qbz5n2kfra8p0\lib\tkinter\__init__.py", 第1921行, 在 __call__中 return self.func(*args) 文件 "C:\Users\user\project\pythonproject\venv\Lib\site-packages\project.py", 第327行, 在 <lambda>中 soundbutton = Button(root, text="upload", command = lambda : upload()) 文件 "C:\Users\user\project\pythonproject\venv\Lib\site-packages\project.py", 第341行, 在 upload中 playsound(file) 文件 "C:\Users\user\project\pythonproject\venv\Lib\site-packages\playsound.py", 第35行, 在 _playsoundWin中 winCommand('open "' + sound + '" alias', alias) 文件 "C:\Users\user\project\pythonproject\venv\Lib\site-packages\playsound.py", 第31行, 在 winCommand中 raise PlaysoundException(exceptionMessage) playsound.PlaysoundException: 命令错误 292: open "" alias playsound_0.39688356388365387 指定的命令需要别名、文件、驱动或设备名称,请提供其中一项。
原因分析
- 判断逻辑错误:当用户取消文件选择时,
askopenfilename返回的是空字符串"",而非None。原代码中if file is not None:的判断无法过滤空字符串,导致空路径被传给playsound(),触发异常。 - 异常捕获失败(若尝试过):可能是未正确导入
PlaysoundException类,或者捕获语句的范围未覆盖到异常触发的位置。
解决方案
方案1:优化文件路径判断(推荐)
直接通过布尔判断过滤空字符串,因为空字符串在Python中会被视为False:
def upload(*args): file = askopenfilename(filetypes= [('MP3 Files', '*.mp3')]) # 仅当file为非空字符串时才执行播放 if file: playsound(file)
方案2:添加异常捕获
如果需要处理播放过程中可能出现的其他异常,可以同时添加try/except,注意要正确导入PlaysoundException:
from playsound import playsound, PlaysoundException # 导入异常类 def upload(*args): file = askopenfilename(filetypes= [('MP3 Files', '*.mp3')]) if file: try: playsound(file) except PlaysoundException as e: print(f"播放失败: {e}")
修改后的完整代码
from tkinter import * from tkinter import ttk from playsound import playsound, PlaysoundException # 导入异常类 from tkinter.filedialog import askopenfilename root = Tk() root.geometry('800x600') frame1 = Frame(root,width='100',height='50',bg='black',borderwidth=1) subframe1 = Frame(root, bg='black', width='660', height='351') labelframe1 = Label(frame1,text='go to frame1',fg='orange',bg='black') frame2 = Frame(root,width='100',height='50',bg='blue') subframe2 = Frame(root, bg='blue', width='660', height='351') labelframe2 = Label(frame2,text='go to frame2',fg='orange',bg='blue') buttonframe1 = Button(subframe1, text="firstbutton",fg='orange') entryframe1 = Entry(subframe1) comboframe1 = ttk.Combobox(subframe1,values=['option1','option2' ,'option3']) comboframe1.set('default option') buttonframe2 = Button(subframe2, text="secondbutton",fg='orange') entryframe2 = Entry(subframe2) comboframe2 = ttk.Combobox(subframe2,values=['option1','option2' ,'option3']) comboframe2.set('default option') uploadbutton = Button(root,text='upload',fg='orange') frame1.place(x=0,y=0) labelframe1.place(relx=0.5,rely=0.5,anchor=CENTER) frame2.place(x=0,y=140) labelframe2.place(relx=0.5,rely=0.5,anchor=CENTER) uploadbutton.place(x=0,y=240) def change_frame1(*args): subframe1.pack(side=RIGHT) subframe2.pack_forget() buttonframe1.place(x=0,y=0) entryframe1.place(x=0,y=40) comboframe1.place(x=0,y=80) def change_frame2(*args): subframe2.pack(side=RIGHT) subframe1.pack_forget() buttonframe2.place(x=0,y=0) entryframe2.place(x=0,y=40) comboframe2.place(x=0,y=80) def upload(*args): file = askopenfilename(filetypes= [('MP3 Files', '*.mp3')]) if file: try: playsound(file) except PlaysoundException as e: print(f"播放出错: {e}") frame1.bind('<Button-1>', change_frame1) labelframe1.bind('<Button-1>', change_frame1) frame2.bind('<Button-1>', change_frame2) labelframe2.bind('<Button-1>', change_frame2) uploadbutton.bind('<Button-1>',upload) root.mainloop()
内容的提问来源于stack exchange,提问作者Meme410
相关产品推荐
相关产品推荐

