customtkinter(以降ctkと略します)で作成したGUIボタンにvtubestudioとvoicevoxのAPIを埋め込み、ボタンが押されたときにvtubestudioとvoicevoxが動作するプログラムを作成したいと考えています。しかし、プロトタイプを作成して動作させると、「Webソケットエラー」が発生します。これはおそらく、APIのサーバー側が処理に追いついていないことを示していると思います。エラーメッセージから、myvts.request(send_hotkey_list)セクションで問題が発生していると思います。これは、WebSocket接続がすでに閉じられているためであり、main()実行の最後にvts接続が切断されているためだと思います。接続が常に開いていることを確認するにはどうすればよいですか?以下はプログラムです
from voicevox_adapter import VoicevoxAdapter
from play_sound import PlaySound
import pyvts
import asyncio
import customtkinter as ctk
input_str = "こんにちは"
va = VoicevoxAdapter()
ps = PlaySound("スピーカー")
data,rate = va.get_voice(input_str)
app = ctk.CTk()
app.geometry("400x240")
plugin_info = {
"plugin_name": "trigger hotkey",
"developer": "OverHome",
"authentication_token_path": "./pyvts_token.txt"
}
async def voice():
ps.play_sound(data,rate)
async def main():
myvts = pyvts.vts(plugin_info=plugin_info)
await myvts.connect()
await myvts.request_authenticate_token() # get token
await myvts.request_authenticate() # use token
response_data = await myvts.request(myvts.vts_request.requestHotKeyList())
hotkey_list = []
for hotkey in response_data['data']['availableHotkeys']:
hotkey_list.append(hotkey['name'])
send_hotkey_request = myvts.vts_request.requestTriggerHotKey(hotkey_list[0])
return myvts,send_hotkey_request
myvts, send_hotkey_list = asyncio.run(main())
async def doing():
await asyncio.sleep(5)
await myvts.request(send_hotkey_list)
await voice()
def doing_def():
asyncio.run(doing())
button = ctk.CTkButton(app,
text="実行",
command=doing_def,
width=200,height=120)
button.grid(column=2,row=1)
app.mainloop()
エラーの全文は次のとおりです
Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Python\lib\site-packages\websockets\legacy\protocol.py", line 963, in transfer_data
message = await self.read_message()
File "C:\Python\lib\site-packages\websockets\legacy\protocol.py", line 1033, in read_message
frame = await self.read_data_frame(max_size=self.max_size)
File "C:\Python\lib\site-packages\websockets\legacy\protocol.py", line 1108, in read_data_frame
frame = await self.read_frame(max_size)
File "C:\Python\lib\site-packages\websockets\legacy\protocol.py", line 1165, in read_frame
frame = await Frame.read(
File "C:\Python\lib\site-packages\websockets\legacy\framing.py", line 68, in read
data = await reader(2)
File "C:\Python\lib\asyncio\streams.py", line 723, in readexactly
await self._wait_for_data('readexactly')
File "C:\Python\lib\asyncio\streams.py", line 517, in _wait_for_data
await self._waiter
asyncio.exceptions.CancelledError
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "C:\Python\lib\tkinter\__init__.py", line 1892, in __call__
return self.func(*args)
File "C:\Python\lib\site-packages\customtkinter\windows\widgets\ctk_button.py", line 554, in _clicked
self._command()
File "c:\Users\HOME anyone\Documents\AIVtuberProject\voicevox\async&ctk.py", line 42, in doing_def
asyncio.run(doing())
File "C:\Python\lib\asyncio\runners.py", line 44, in run
return loop.run_until_complete(main)
File "C:\Python\lib\asyncio\base_events.py", line 616, in run_until_complete
return future.result()
File "c:\Users\HOME anyone\Documents\AIVtuberProject\voicevox\async&ctk.py", line 38, in doing
await myvts.request(send_hotkey_list)
File "C:\Python\lib\site-packages\pyvts\vts.py", line 112, in request
await self.websocket.send(json.dumps(request_msg))
File "C:\Python\lib\site-packages\websockets\legacy\protocol.py", line 635, in send
await self.ensure_open()
File "C:\Python\lib\site-packages\websockets\legacy\protocol.py", line 939, in ensure_open
raise self.connection_closed_exc()
websockets.exceptions.ConnectionClosedError: no close frame received or sent