99from pathlib import Path
1010from gameScoreBoard import gameScoreBoardManager
1111from country_name import country_name
12- import os
12+ import os , sys
1313from typing import List , Tuple
1414
1515version = "元3.2.2"
1616
17+ def resource_path (relative_path : str ) -> Path :
18+ """获取资源文件路径(开发环境 + PyInstaller)
19+ 用于qm文件和media
20+ """
21+ base_path = Path (getattr (sys , "_MEIPASS" , Path (__file__ ).resolve ().parent ))
22+ return base_path / relative_path
23+
24+
1725class IniConfig :
1826 def __init__ (self , file_path ):
1927 """
@@ -175,6 +183,7 @@ def __init__(self, MainWindow, args):
175183 # 没权限,改用 %APPDATA%\你的程序名\
176184 self .setting_path = Path (os .environ ['APPDATA' ]) / ('MetaMineSweeper' + version [1 :])
177185 self .setting_path .mkdir (parents = True , exist_ok = True )
186+ # r_path是打包后_internal外面那一层,和exe同一级
178187 self .r_path = r_path
179188
180189 # 录像保存位置
@@ -189,22 +198,22 @@ def __init__(self, MainWindow, args):
189198 self .record_setting = IniConfig (record_path )
190199
191200
192- self .ico_path = str (r_path . with_name ('media' ). joinpath ( 'cat.ico' ) )
193- self .smileface_path = str (r_path . with_name ('media' ). joinpath ( 'smileface.svg' ) )
194- self .clickface_path = str (r_path . with_name ('media' ). joinpath ( 'clickface.svg' ) )
195- self .lostface_path = str (r_path . with_name ('media' ). joinpath ( 'lostface.svg' ) )
196- self .winface_path = str (r_path . with_name ('media' ). joinpath ( 'winface.svg' ) )
197- self .smilefacedown_path = str (r_path . with_name ('media' ). joinpath ( 'smilefacedown.svg' ) )
198- self .LED0_path = str (r_path . with_name ('media' ). joinpath ( 'LED0.png' ) )
199- self .LED1_path = str (r_path . with_name ('media' ). joinpath ( 'LED1.png' ) )
200- self .LED2_path = str (r_path . with_name ('media' ). joinpath ( 'LED2.png' ) )
201- self .LED3_path = str (r_path . with_name ('media' ). joinpath ( 'LED3.png' ) )
202- self .LED4_path = str (r_path . with_name ('media' ). joinpath ( 'LED4.png' ) )
203- self .LED5_path = str (r_path . with_name ('media' ). joinpath ( 'LED5.png' ) )
204- self .LED6_path = str (r_path . with_name ('media' ). joinpath ( 'LED6.png' ) )
205- self .LED7_path = str (r_path . with_name ('media' ). joinpath ( 'LED7.png' ) )
206- self .LED8_path = str (r_path . with_name ('media' ). joinpath ( 'LED8.png' ) )
207- self .LED9_path = str (r_path . with_name ('media' ). joinpath ( 'LED9.png' ) )
201+ self .ico_path = str (resource_path ('media' ) / 'cat.ico' )
202+ self .smileface_path = str (resource_path ('media' ) / 'smileface.svg' )
203+ self .clickface_path = str (resource_path ('media' ) / 'clickface.svg' )
204+ self .lostface_path = str (resource_path ('media' ) / 'lostface.svg' )
205+ self .winface_path = str (resource_path ('media' ) / 'winface.svg' )
206+ self .smilefacedown_path = str (resource_path ('media' ) / 'smilefacedown.svg' )
207+ self .LED0_path = str (resource_path ('media' ) / 'LED0.png' )
208+ self .LED1_path = str (resource_path ('media' ) / 'LED1.png' )
209+ self .LED2_path = str (resource_path ('media' ) / 'LED2.png' )
210+ self .LED3_path = str (resource_path ('media' ) / 'LED3.png' )
211+ self .LED4_path = str (resource_path ('media' ) / 'LED4.png' )
212+ self .LED5_path = str (resource_path ('media' ) / 'LED5.png' )
213+ self .LED6_path = str (resource_path ('media' ) / 'LED6.png' )
214+ self .LED7_path = str (resource_path ('media' ) / 'LED7.png' )
215+ self .LED8_path = str (resource_path ('media' ) / 'LED8.png' )
216+ self .LED9_path = str (resource_path ('media' ) / 'LED9.png' )
208217
209218
210219 self .mainWindow .setWindowIcon (QIcon (self .ico_path ))
@@ -447,7 +456,7 @@ def trans_language(self, language = ""):
447456 language = self .language
448457 app = QApplication .instance ()
449458 if language != "zh_CN" :
450- self .trans .load (str (self . r_path . with_name (language + '.qm' )))
459+ self .trans .load (str (resource_path (language + '.qm' )))
451460 app .installTranslator (self .trans )
452461 self .retranslateUi (self .mainWindow )
453462 self .score_board_manager .retranslateUi (self .score_board_manager .ui .QWidget )
@@ -566,15 +575,15 @@ def set_country_flag(self, country = None):
566575 if country not in country_name :
567576 country = country .capitalize ()
568577 if country not in country_name :
569- file_path = self . r_path . with_name ('media' ) / (country .lower () + ".svg" )
578+ file_path = resource_path ('media' ) / (country .lower () + ".svg" )
570579 if os .path .exists (file_path ):
571580 flag_name = file_path
572581 else :
573582 self .label_flag .clear ()
574583 self .label_flag .update ()
575584 return
576585 else :
577- flag_name = self . r_path . with_name ('media' ) / (country_name [country ] + ".svg" )
586+ flag_name = resource_path ('media' ) / (country_name [country ] + ".svg" )
578587 pixmap = QPixmap (str (flag_name )).scaled (51 , 31 )
579588 self .label_flag .setPixmap (pixmap )
580589 self .label_flag .update ()
0 commit comments