@@ -108,7 +108,7 @@ def initUI(self):
108108 get_content_pushbutton_name_async ("lottery" , "reset_button" )
109109 )
110110 self ._set_widget_font (self .reset_button , 15 )
111- self .reset_button .setFixedSize ( 165 , 45 )
111+ self .reset_button .setFixedHeight ( 45 )
112112 self .reset_button .clicked .connect (lambda : self .reset_count ())
113113
114114 self .minus_button = PushButton ("-" )
@@ -141,21 +141,24 @@ def initUI(self):
141141 self .count_widget = QWidget ()
142142 horizontal_layout = QHBoxLayout ()
143143 horizontal_layout .setContentsMargins (0 , 0 , 0 , 0 )
144- horizontal_layout .addWidget (self .minus_button , 0 , Qt .AlignmentFlag .AlignLeft )
145- horizontal_layout .addWidget (self .count_label , 0 , Qt .AlignmentFlag .AlignLeft )
146- horizontal_layout .addWidget (self .plus_button , 0 , Qt .AlignmentFlag .AlignLeft )
144+ horizontal_layout .setSpacing (0 )
145+ horizontal_layout .addWidget (self .minus_button )
146+ horizontal_layout .addStretch ()
147+ horizontal_layout .addWidget (self .count_label )
148+ horizontal_layout .addStretch ()
149+ horizontal_layout .addWidget (self .plus_button )
147150 self .count_widget .setLayout (horizontal_layout )
148151
149152 self .start_button = PrimaryPushButton (
150153 get_content_pushbutton_name_async ("lottery" , "start_button" )
151154 )
152155 self ._set_widget_font (self .start_button , 15 )
153- self .start_button .setFixedSize ( 165 , 45 )
156+ self .start_button .setFixedHeight ( 45 )
154157 self .start_button .clicked .connect (lambda : self .start_draw ())
155158
156159 self .pool_list_combobox = ComboBox ()
157160 self ._set_widget_font (self .pool_list_combobox , 12 )
158- self .pool_list_combobox .setFixedSize ( 165 , 45 )
161+ self .pool_list_combobox .setFixedHeight ( 45 )
159162 self .pool_list_combobox .setPlaceholderText (
160163 get_content_name_async ("lottery" , "default_empty_item" )
161164 )
@@ -164,27 +167,27 @@ def initUI(self):
164167
165168 self .list_combobox = ComboBox ()
166169 self ._set_widget_font (self .list_combobox , 12 )
167- self .list_combobox .setFixedSize ( 165 , 45 )
170+ self .list_combobox .setFixedHeight ( 45 )
168171 # 延迟填充班级列表,避免启动时进行文件IO
169172 self .list_combobox .currentTextChanged .connect (self .on_class_changed )
170173
171174 self .range_combobox = ComboBox ()
172175 self ._set_widget_font (self .range_combobox , 12 )
173- self .range_combobox .setFixedSize ( 165 , 45 )
176+ self .range_combobox .setFixedHeight ( 45 )
174177 # 延迟填充范围选项
175178 self .range_combobox .currentTextChanged .connect (self .on_filter_changed )
176179
177180 self .gender_combobox = ComboBox ()
178181 self ._set_widget_font (self .gender_combobox , 12 )
179- self .gender_combobox .setFixedSize ( 165 , 45 )
182+ self .gender_combobox .setFixedHeight ( 45 )
180183 # 延迟填充性别选项
181184 self .gender_combobox .currentTextChanged .connect (self .on_filter_changed )
182185
183186 self .remaining_button = PushButton (
184187 get_content_pushbutton_name_async ("lottery" , "remaining_button" )
185188 )
186189 self ._set_widget_font (self .remaining_button , 12 )
187- self .remaining_button .setFixedSize ( 165 , 45 )
190+ self .remaining_button .setFixedHeight ( 45 )
188191 self .remaining_button .clicked .connect (lambda : self .show_remaining_list ())
189192
190193 # 初始时不进行昂贵的数据加载,改为延迟填充
@@ -197,7 +200,6 @@ def initUI(self):
197200 self .many_count_label = BodyLabel (formatted_text )
198201 self .many_count_label .setAlignment (Qt .AlignmentFlag .AlignCenter )
199202 self ._set_widget_font (self .many_count_label , 10 )
200- self .many_count_label .setFixedWidth (165 )
201203
202204 self .control_widget = QWidget ()
203205 self .control_layout = QVBoxLayout (self .control_widget )
@@ -285,6 +287,9 @@ def initUI(self):
285287 main_layout .addWidget (scroll , 1 )
286288 main_layout .addWidget (self .control_widget )
287289
290+ # 统一调整控件宽度以适应文本内容
291+ self ._adjustControlWidgetWidths ()
292+
288293 # 在事件循环中延迟填充下拉框和初始统计,减少启动阻塞
289294 QTimer .singleShot (0 , self .populate_lists )
290295
@@ -301,6 +306,54 @@ def add_control_widget_if_enabled(
301306 # 出错时默认添加控件
302307 layout .addWidget (widget , alignment = Qt .AlignmentFlag .AlignCenter )
303308
309+ def _adjustControlWidgetWidths (self ):
310+ """统一调整控件宽度以适应文本内容"""
311+ try :
312+ # 收集所有需要调整宽度的控件
313+ widgets_to_adjust = [
314+ self .reset_button ,
315+ self .count_widget ,
316+ self .start_button ,
317+ self .pool_list_combobox ,
318+ self .list_combobox ,
319+ self .range_combobox ,
320+ self .gender_combobox ,
321+ self .remaining_button ,
322+ self .many_count_label ,
323+ ]
324+
325+ # 计算所有控件文本所需的最大宽度
326+ max_text_width = 0
327+ for widget in widgets_to_adjust :
328+ fm = widget .fontMetrics ()
329+ # 检查按钮/标签文本
330+ if hasattr (widget , "text" ) and widget .text ():
331+ text_width = fm .horizontalAdvance (widget .text ())
332+ max_text_width = max (max_text_width , text_width )
333+ # 检查占位符文本
334+ if hasattr (widget , "placeholderText" ) and widget .placeholderText ():
335+ text_width = fm .horizontalAdvance (widget .placeholderText ())
336+ max_text_width = max (max_text_width , text_width )
337+ # 检查下拉框所有选项的宽度
338+ if hasattr (widget , "count" ):
339+ for i in range (widget .count ()):
340+ item_text = widget .itemText (i )
341+ if item_text :
342+ text_width = fm .horizontalAdvance (item_text )
343+ max_text_width = max (max_text_width , text_width )
344+
345+ # 计算统一宽度(文本宽度 + 边距 + 下拉框箭头空间)
346+ padding = 60 # 左右边距 + 下拉箭头空间
347+ min_width = 165 # 最小宽度
348+ unified_width = max (min_width , max_text_width + padding )
349+
350+ # 设置所有控件的固定宽度
351+ for widget in widgets_to_adjust :
352+ widget .setFixedWidth (int (unified_width ))
353+
354+ except Exception as e :
355+ logger .debug (f"调整控件宽度时出错: { e } " )
356+
304357 def on_pool_changed (self ):
305358 """当奖池选择改变时,更新奖数显示"""
306359 try :
@@ -961,6 +1014,9 @@ def populate_lists(self):
9611014
9621015 LotteryUtils .update_start_button_state (self .start_button , total_count )
9631016
1017+ # 重新调整控件宽度以适应下拉框内容
1018+ self ._adjustControlWidgetWidths ()
1019+
9641020 except Exception as e :
9651021 logger .error (f"延迟填充列表失败: { e } " )
9661022
0 commit comments