Skip to content

Commit cb7e18c

Browse files
committed
perf:优化资源路径写法
1 parent 48d25b2 commit cb7e18c

File tree

4 files changed

+30
-33
lines changed

4 files changed

+30
-33
lines changed

src/main.py

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,20 +16,11 @@
1616
from mp_plugins.events import *
1717
from mp_plugins import PluginManager
1818
from pathlib import Path
19-
# import os
2019
from utils import get_paths, patch_env
2120

2221
os.environ["QT_FONT_DPI"] = "96"
2322

2423

25-
# def patch_env():
26-
# import os
27-
28-
29-
# env = os.environ.copy()
30-
# root = os.path.dirname(os.path.abspath(__file__)) # 你的项目根目录
31-
# env["PYTHONPATH"] = root
32-
# return env
3324

3425

3526
def on_new_connection(localServer: QLocalServer):

src/mineSweeperGUI.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -642,7 +642,7 @@ def dump_evf_file_data(self):
642642
if not country:
643643
country = "XX"
644644
elif len(country) == 2 and country.isalpha() and country.isascii():
645-
file_path = self.r_path.with_name('media') / (country.lower() + ".svg")
645+
file_path = superGUI.resource_path('media') / (country.lower() + ".svg")
646646
if os.path.exists(file_path):
647647
country = country.upper()
648648
elif country in country_name:

src/superGUI.py

Lines changed: 29 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,19 @@
99
from pathlib import Path
1010
from gameScoreBoard import gameScoreBoardManager
1111
from country_name import country_name
12-
import os
12+
import os, sys
1313
from typing import List, Tuple
1414

1515
version = "元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+
1725
class 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()

src/ui/uiComponents.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -267,9 +267,6 @@ def __init__(self, countries, parent=None):
267267
QComboBox QAbstractItemView {
268268
text-align: center;
269269
}
270-
QComboBox {
271-
qproperty-alignment: 'AlignCenter';
272-
}
273270
""")
274271

275272
def _on_text_edited(self, text):

0 commit comments

Comments
 (0)