如何为消息接收功能添加声音

时间:2018-03-14 19:20:42

标签: python python-3.x tkinter

我目前正在开发一个基于Python的聊天程序,我试图弄清楚如何在用户收到消息时添加通知声音,这是客户端的消息接收功能我想要的我希望当用户收到消息时会发出声音

def receive():
    """Handles receiving of messages."""
    while True:
        try:
            msg = client_socket.recv(BUFSIZ).decode("utf8")
            msg_list.insert(tkinter.END, msg)
        except OSError:  # Possibly client has left the chat.
            break

1 个答案:

答案 0 :(得分:0)

让我们先创建一个播放声音的功能

private Status status; 

public Status getStatus(){
  return this.status;
}

在此处详细了解import playsound def play(): ''' Setting second argument to False makes the function run asynchronously. ''' isSync = False playsound.playsound("/path/to/your/sound/file.mp3",isSync) '''In your function to handle incoming messages''' def receive(): """Handles receiving of messages.""" while True: try: msg = client_socket.recv(BUFSIZ).decode("utf8") msg_list.insert(tkinter.END, msg) play() except OSError: # Possibly client has left the chat. break https://pypi.python.org/pypi/playsound/1.2.1 \

编辑:修正缩进错误

P.S。 :感谢@Nae建议改进异步部分。