@@ -812,43 +812,65 @@ def update_content(
812812 if not student_labels :
813813 return
814814
815- # 清除所有旧控件
816- while self .content_layout .count ():
817- item = self .content_layout .takeAt (0 )
818- widget = item .widget ()
819- if widget :
820- widget .deleteLater ()
821-
822- # 添加新控件
815+ # 预计算窗口大小
816+ total_height = 0
817+ max_width = 0
823818 for label in student_labels :
824- # 如果有字体设置,则应用到标签上
819+ # 应用字体设置
825820 if font_settings_group :
826- # 检查是否使用全局字体
827821 use_global_font = readme_settings_async (
828822 font_settings_group , "use_global_font"
829823 )
830824 custom_font = None
831- if use_global_font == 1 : # 不使用全局字体,使用自定义字体
825+ if use_global_font == 1 :
832826 custom_font = readme_settings_async (
833827 font_settings_group , "custom_font"
834828 )
835829 if custom_font and hasattr (label , "setStyleSheet" ):
836- # 获取当前样式表并添加字体设置
837830 current_style = label .styleSheet ()
838831 label .setStyleSheet (
839832 f"font-family: '{ custom_font } '; { current_style } "
840833 )
834+ # 计算标签大小
835+ label .adjustSize ()
836+ size_hint = label .sizeHint ()
837+ total_height += size_hint .height ()
838+ max_width = max (max_width , size_hint .width ())
839+
840+ # 加上布局间距和边距
841+ spacing = self .content_layout .spacing ()
842+ margins = self .content_layout .contentsMargins ()
843+ total_height += spacing * (len (student_labels ) - 1 )
844+ total_height += margins .top () + margins .bottom ()
845+ max_width += margins .left () + margins .right ()
846+
847+ # 加上倒计时标签的高度
848+ if self .countdown_label :
849+ countdown_height = self .countdown_label .sizeHint ().height ()
850+ total_height += countdown_height + spacing
851+
852+ # 清除所有旧控件
853+ while self .content_layout .count ():
854+ item = self .content_layout .takeAt (0 )
855+ widget = item .widget ()
856+ if widget :
857+ widget .deleteLater ()
858+
859+ # 添加新控件
860+ for label in student_labels :
841861 self .content_layout .addWidget (label )
842862
863+ # 设置窗口大小
864+ window_width = max (300 , max_width + 30 )
865+ window_height = min (600 , total_height + 30 )
866+ self .setFixedSize (window_width , window_height )
867+
843868 # 确保颜色与当前主题同步
844869 try :
845870 self ._on_theme_changed ()
846871 except Exception as e :
847872 logger .exception ("更新内容时同步主题时出错(已忽略): {}" , e )
848873
849- # 调整窗口大小以适应内容
850- self .adjustSize ()
851-
852874 # 检查窗口是否已经显示
853875 if self .isVisible ():
854876 # 如果窗口已经显示,只更新内容和透明度,不重新定位窗口
0 commit comments