@@ -13,40 +13,69 @@ def __init__(self, parent=None):
13
13
14
14
def init_ui (self ):
15
15
self .setWindowTitle ("NCM转换器" )
16
- self .setGeometry (100 , 100 , 400 , 400 ) # 调整窗体尺寸
17
-
16
+ self .setGeometry (100 , 100 , 400 , 400 )
17
+ self . setMinimumSize ( 200 , 200 )
18
18
self .setWindowIcon (QIcon (self .get_resource_path ("file/favicon-32x32.png" )))
19
19
# 加载图片
20
- self .original_pixmap = QPixmap (self .get_resource_path ("file/bk.png" ))
21
-
22
- self .label = QLabel (self )
23
- self .label .setGeometry (0 , 0 , 400 , 400 ) # 调整标签尺寸
24
- self .update_text ('将ncm文件拖拽到此处' ) # 初始文本内容
25
- def create_base_pixmap (self ):
26
- """创建不包含文本的基础背景图片"""
27
- self .base_pixmap = QPixmap (self .get_resource_path ("file/bk.png" ))
20
+ self .original_pixmap = QPixmap (self .get_resource_path ("file/bk_l.png" ))
21
+
22
+ # 修改标签布局方式
23
+ layout = QVBoxLayout ()
28
24
self .label = QLabel (self )
29
- self . label . setPixmap (self .base_pixmap )
30
- self .label . setGeometry ( 0 , 0 , 573 , 573 )
25
+ layout . addWidget (self .label )
26
+ self .setLayout ( layout )
31
27
28
+ self .update_text ('将ncm文件拖拽到此处' )
29
+
30
+ # 添加 resizeEvent 处理函数
31
+ def resizeEvent (self , event ):
32
+ super ().resizeEvent (event )
33
+ # 当窗口大小改变时,更新文本显示
34
+ self .update_text (self .label .text ())
35
+
32
36
def update_text (self , text ):
33
37
resized_pixmap = self .original_pixmap .scaled (self .label .size (), Qt .AspectRatioMode .KeepAspectRatio , Qt .TransformationMode .SmoothTransformation )
34
- pixmap = resized_pixmap .copy () # 复制调整大小后的 QPixmap 对象
38
+ pixmap = resized_pixmap .copy ()
35
39
painter = QPainter (pixmap )
36
- painter .setPen (QColor ('black' )) # 设置文本颜色
37
- font = QFont ('SimHei' , 20 ) # 设置字体和大小
38
- painter .setFont (font )
40
+ painter .setPen (QColor ('black' ))
39
41
42
+ # 计算合适的字体大小
43
+ font_size = 16
44
+ max_width = pixmap .width () * 0.9 # 留出10%边距
45
+ font = QFont ('SimHei' , font_size )
40
46
font_metrics = QFontMetrics (font )
41
- text_width = font_metrics .horizontalAdvance (text )
42
- text_height = font_metrics .height ()
47
+
48
+ # 如果文本太长,进行截断处理
49
+ text_lines = []
50
+ if len (text ) > 30 : # 如果文本长度超过30个字符
51
+ words = text .split (':' , 1 ) # 在":"处分割
52
+ if len (words ) > 1 :
53
+ text_lines .append (words [0 ] + ':' )
54
+ remaining_text = words [1 ]
55
+ # 每行最多显示20个字符
56
+ while remaining_text :
57
+ if len (remaining_text ) > 20 :
58
+ text_lines .append (remaining_text [:20 ])
59
+ remaining_text = remaining_text [20 :]
60
+ else :
61
+ text_lines .append (remaining_text )
62
+ break
63
+ else :
64
+ text_lines = [text ]
65
+
66
+ # 绘制多行文本
67
+ painter .setFont (font )
68
+ total_height = len (text_lines ) * font_metrics .height ()
69
+ current_y = (pixmap .height () - total_height ) // 2 + font_metrics .ascent ()
43
70
44
- x = (pixmap .width () - text_width ) // 2
45
- y = (pixmap .height () - text_height ) // 2 + font_metrics .ascent ()
71
+ for line in text_lines :
72
+ text_width = font_metrics .horizontalAdvance (line )
73
+ x = (pixmap .width () - text_width ) // 2
74
+ painter .drawText (x , current_y , line )
75
+ current_y += font_metrics .height ()
46
76
47
- painter .drawText (x , y , text ) # 在图片中居中绘制文本
48
77
painter .end ()
49
- self .label .setPixmap (pixmap ) # 更新 QLabel 中的图片
78
+ self .label .setPixmap (pixmap )
50
79
51
80
def get_resource_path (self ,relative_path ):
52
81
if hasattr (sys , '_MEIPASS' ):
0 commit comments