@@ -52,17 +52,30 @@ async def disconnect(self):
5252 断开连接
5353 """
5454 logger .info ("字幕执行器正在断开连接..." )
55- self .running = False
56-
57- # 取消订阅
55+ # 先取消订阅,这样不会再有新消息
5856 await self .synapse .unsubscribe_output (self ._handle_output )
57+ logger .info ("字幕执行器已取消订阅输出" )
5958
60- # 关闭字幕
61- if self .subtitle :
62- self .subtitle .close ()
63- self .subtitle = None
59+ # 停止处理消息的循环
60+ self .running = False
6461
65- logger .info ("字幕执行器已断开连接" )
62+ # 关闭字幕
63+ try :
64+ if self .subtitle and hasattr (self .subtitle , "close" ):
65+ self .subtitle .close ()
66+ self .subtitle = None
67+ logger .info ("字幕窗口已关闭" )
68+ except Exception as e :
69+ logger .error (f"关闭字幕窗口时出错: { e } " )
70+
71+ # 最多等待1秒让线程自行结束
72+ if self .subtitle_thread and self .subtitle_thread .is_alive ():
73+ logger .info ("等待字幕线程结束..." )
74+ self .subtitle_thread .join (timeout = 1.0 )
75+ if self .subtitle_thread .is_alive ():
76+ logger .warning ("字幕线程未能正常结束,但将继续关闭流程" )
77+
78+ logger .info ("字幕执行器已完全断开连接" )
6679
6780 async def _handle_output (self , neurotransmitter : Neurotransmitter ):
6881 """
@@ -84,32 +97,52 @@ def _run_subtitle(self):
8497 """
8598 运行字幕窗口
8699 """
87- # 创建字幕
88- self .subtitle = MultiMessageSubtitle (
89- text = "等待消息..." ,
90- theme = "dark" ,
91- font_family = "Microsoft YaHei" ,
92- font_size = 24 ,
93- text_color = "#FFFFFF" ,
94- bg_color = "#333333" ,
95- opacity = 0.7 ,
96- animation_speed = 10 ,
97- border_radius = 10 ,
98- padding = 15 ,
99- show_history = self .show_history ,
100- )
101-
102- # 设置初始位置(屏幕底部)
103- screen_width = self .subtitle .root .winfo_screenwidth ()
104- screen_height = self .subtitle .root .winfo_screenheight ()
105- subtitle_width = 600
106- subtitle_height = 200
107- x_pos = (screen_width - subtitle_width ) // 2
108- y_pos = screen_height - subtitle_height - 50
109- self .subtitle .root .geometry (f"{ subtitle_width } x{ subtitle_height } +{ x_pos } +{ y_pos } " )
110-
111- # 运行字幕
112- self .subtitle .run ()
100+ try :
101+ # 创建字幕
102+ self .subtitle = MultiMessageSubtitle (
103+ text = "等待消息..." ,
104+ theme = "dark" ,
105+ font_family = "Microsoft YaHei" ,
106+ font_size = 24 ,
107+ text_color = "#FFFFFF" ,
108+ bg_color = "#333333" ,
109+ opacity = 0.7 ,
110+ animation_speed = 10 ,
111+ border_radius = 10 ,
112+ padding = 15 ,
113+ show_history = self .show_history ,
114+ )
115+
116+ # 设置初始位置(屏幕底部)
117+ screen_width = self .subtitle .root .winfo_screenwidth ()
118+ screen_height = self .subtitle .root .winfo_screenheight ()
119+ subtitle_width = 600
120+ subtitle_height = 200
121+ x_pos = (screen_width - subtitle_width ) // 2
122+ y_pos = screen_height - subtitle_height - 50
123+ self .subtitle .root .geometry (f"{ subtitle_width } x{ subtitle_height } +{ x_pos } +{ y_pos } " )
124+
125+ # 添加周期性检查,当running变为False时退出循环
126+ def check_running ():
127+ if not self .running and self .subtitle and self .subtitle .root :
128+ try :
129+ self .subtitle .root .quit ()
130+ return # 不再继续调度
131+ except Exception as e :
132+ logger .error (f"退出字幕循环时出错: { e } " )
133+
134+ # 如果仍在运行,继续检查
135+ if self .subtitle and self .subtitle .root :
136+ self .subtitle .root .after (100 , check_running )
137+
138+ # 开始检查循环
139+ self .subtitle .root .after (100 , check_running )
140+
141+ # 运行字幕
142+ self .subtitle .run ()
143+
144+ except Exception as e :
145+ logger .error (f"字幕线程出错: { e } " , exc_info = True )
113146
114147 async def _process_messages (self ):
115148 """
0 commit comments