From 5ee49dfc45661a18b85d0e81266c3e520721ee1c Mon Sep 17 00:00:00 2001 From: Randy Mackay Date: Wed, 13 Dec 2023 13:33:54 +0900 Subject: [PATCH] chat: another attempt to fix windows crashing --- MAVProxy/modules/mavproxy_chat/__init__.py | 8 ++++++- MAVProxy/modules/mavproxy_chat/chat_window.py | 23 ++++++++----------- 2 files changed, 16 insertions(+), 15 deletions(-) diff --git a/MAVProxy/modules/mavproxy_chat/__init__.py b/MAVProxy/modules/mavproxy_chat/__init__.py index 7ef1bab67d..c18849e07c 100644 --- a/MAVProxy/modules/mavproxy_chat/__init__.py +++ b/MAVProxy/modules/mavproxy_chat/__init__.py @@ -12,6 +12,7 @@ from MAVProxy.modules.lib import mp_module from MAVProxy.modules.lib import mp_util from MAVProxy.modules.mavproxy_chat import chat_window +from threading import Thread class chat(mp_module.MPModule): def __init__(self, mpstate): @@ -25,7 +26,12 @@ def __init__(self, mpstate): # keep reference to mpstate self.mpstate = mpstate - # add menu to map and console + # run chat window in a separate thread + self.thread = Thread(target=self.create_chat_window) + self.thread.start() + + # create chat window (should be called from a new thread) + def create_chat_window(self): if mp_util.has_wxpython: # create chat window self.chat_window = chat_window.chat_window(self.mpstate) diff --git a/MAVProxy/modules/mavproxy_chat/chat_window.py b/MAVProxy/modules/mavproxy_chat/chat_window.py index f447dd4dc0..54168ad0df 100644 --- a/MAVProxy/modules/mavproxy_chat/chat_window.py +++ b/MAVProxy/modules/mavproxy_chat/chat_window.py @@ -17,6 +17,13 @@ def __init__(self, mpstate): # lock to prevent multiple threads sending text to the assistant at the same time self.send_lock = Lock() + # create chat_openai object + self.chat_openai = chat_openai.chat_openai(self.mpstate) + + # create chat_voice_to_text object + self.chat_voice_to_text = chat_voice_to_text.chat_voice_to_text() + + # create chat window self.app = wx.App() self.frame = wx.Frame(None, title="Chat", size=(650, 200)) @@ -66,24 +73,12 @@ def __init__(self, mpstate): # show frame self.frame.Show() - # create chat_openai object - self.chat_openai = chat_openai.chat_openai(self.mpstate) - - # create chat_voice_to_text object - self.chat_voice_to_text = chat_voice_to_text.chat_voice_to_text() - - # run chat window in a separate thread - self.thread = Thread(target=self.idle_task) - self.thread.start() - - # update function rapidly called by mavproxy - def idle_task(self): - # update chat window + # chat window loop (this does not return until the window is closed) self.app.MainLoop() # show the chat window def show(self): - self.frame.Show() + wx.CallAfter(self.frame.Show()) # hide the chat window def hide(self):