Skip to content

Commit 2383476

Browse files
author
taojiayuan
committed
improve assets
1 parent 423ead4 commit 2383476

File tree

7 files changed

+17
-4
lines changed

7 files changed

+17
-4
lines changed

UIFunc.py

+17-4
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import json
55
import os
66
import re
7+
import sys
78
import threading
89
import time
910
import traceback
@@ -44,9 +45,21 @@ def current_ts():
4445
return int(time.time() * 1000)
4546

4647

48+
def get_assets_path(*paths):
49+
# pyinstaller -F --add-data ./assets;assets KeymouseGo.py
50+
try:
51+
root = sys._MEIPASS
52+
except:
53+
root = os.getcwd()
54+
return os.path.join(root, 'assets', *paths)
55+
56+
4757
class UIFunc(QMainWindow, Ui_UIView):
4858
def __init__(self):
4959
super(UIFunc, self).__init__()
60+
61+
print('assets root:', get_assets_path())
62+
5063
self.setupUi(self)
5164

5265
self.config = self.loadconfig()
@@ -331,12 +344,12 @@ def onconfigchange(self):
331344

332345
def onchangelang(self):
333346
if self.choice_language.currentText() == '简体中文':
334-
self.trans.load('i18n/zh-cn')
347+
self.trans.load(get_assets_path('i18n', 'zh-cn'))
335348
_app = QApplication.instance()
336349
_app.installTranslator(self.trans)
337350
self.retranslateUi(self)
338351
elif self.choice_language.currentText() == 'English':
339-
self.trans.load('i18n/en')
352+
self.trans.load(get_assets_path('i18n', 'en'))
340353
_app = QApplication.instance()
341354
_app.installTranslator(self.trans)
342355
self.retranslateUi(self)
@@ -646,15 +659,15 @@ def run(self):
646659

647660
def _play_start_sound(self):
648661
try:
649-
path = os.path.join(os.getcwd(), 'sounds', 'start.mp3')
662+
path = get_assets_path('sounds', 'start.mp3')
650663
playsound(path)
651664
except PlaysoundException as e:
652665
print(e)
653666

654667
@classmethod
655668
def play_end_sound(cls):
656669
try:
657-
path = os.path.join(os.getcwd(), 'sounds', 'end.mp3')
670+
path = get_assets_path('sounds', 'end.mp3')
658671
playsound(path)
659672
except PlaysoundException as e:
660673
print(e)
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

0 commit comments

Comments
 (0)