好友
阅读权限 10
听众
最后登录 1970-1-1
sjnh
发表于 2023-3-1 01:27
50 吾爱币
本帖最后由 sjnh 于 2023-3-1 11:35 编辑
自己用python封装了一个exe文件之后,点击运行,发生如下错误:
Exception in thread Thread-2:
Traceback (most recent call last):
File "threading.py", line 932, in _bootstrap_inner
File "threading.py", line 870, in run
File "playsound.py", line 35, in _playsoundWin
File "playsound.py", line 30, in winCommand
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xd5 in position 0: invalid continuation byte
python源码如下:
#-*- coding : utf-8 -*-
# coding:unicode_escape
import os.path
import sys
from pynput import keyboard
from playsound import playsound
from threading import Thread
def onRelease(key):
dic_sound = {
"a" : resource_path(os.path.join("key" ,"A.mp3" )),
"b" : resource_path(os.path.join("key" ,"B.mp3" )),
"c" : resource_path(os.path.join("key" ,"C.mp3" )),
"d" : resource_path(os.path.join("key" ,"D.mp3" )),
"e" : resource_path(os.path.join("key" ,"E.mp3" )),
"f" : resource_path(os.path.join("key" ,"F.mp3" )),
"g" : resource_path(os.path.join("key" ,"g.mp3" )),
"j" : resource_path(os.path.join("key" ,"j.mp3" )),
"n" : resource_path(os.path.join("key" ,"n.mp3" ))
}
try :
#print(f" 用户输入: {key.char}")
if key.char in "abcdefgjn" :
audio = dic_sound[key.char]
else :
audio = ""
except AttributeError :
#print(f" 用户输入: {key.name}")
if key.name == keyboard.Key.esc:
audio = dic_sound["a" ]
else :
audio = ""
if audio != "" :
t = Thread(target =playsound,args =(audio,))
t.start()
def resource_path(relative_path):
if getattr (sys,"frozen" ,False ):
base_path = sys._MEIPASS
else :
base_path = os.path.abspath("." )
return os.path.join(base_path,relative_path)
listener = keyboard.Listener(on_release =onRelease)
listener.start()
listener.join()
最佳答案
查看完整内容
修改playsound.py文件
1、如果你用PyCharm,就按住CTRL 再点击 from playsound import playsound 的playsound
2、修改55行,可以注释掉 #command = ' '.join(command).encode('utf-16')
改为:command = ' '.join(command)
3、重新打包,打包的话 记得把修改spec ,把资源文件 也就是你的 MP3文件都打包进去。
缺点:多次按键 会直接重复叠加播放,不会结束之 ...
发帖前要善用【论坛搜索 】 功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。