feat: 支持外部加载音频文件,优化打包配置
This commit is contained in:
30
countdown.py
30
countdown.py
@@ -17,6 +17,18 @@ class ConfigDialog(QDialog):
|
||||
self.init_ui()
|
||||
|
||||
def init_ui(self):
|
||||
self.setStyleSheet("""
|
||||
QDialog {
|
||||
background-color: #f5f5f5;
|
||||
}
|
||||
QLabel {
|
||||
color: #333333;
|
||||
font-size: 14px;
|
||||
}
|
||||
QSpinBox, QCheckBox {
|
||||
padding: 5px;
|
||||
}
|
||||
""")
|
||||
layout = QFormLayout(self)
|
||||
|
||||
# 自定义倒计时
|
||||
@@ -181,11 +193,23 @@ class TimerApp(QWidget):
|
||||
|
||||
def setup_audio(self):
|
||||
self.sound = QSoundEffect()
|
||||
|
||||
# 优先从外部目录加载音频文件
|
||||
if getattr(sys, 'frozen', False):
|
||||
base_path = sys._MEIPASS
|
||||
# exe所在目录
|
||||
external_path = os.path.dirname(sys.executable)
|
||||
else:
|
||||
base_path = os.path.dirname(__file__)
|
||||
alarm_file = os.path.join(base_path, "alarm.wav")
|
||||
# 当前工作目录
|
||||
external_path = os.getcwd()
|
||||
|
||||
alarm_file = os.path.join(external_path, "alarm.wav")
|
||||
if not os.path.exists(alarm_file):
|
||||
# 外部没有则从打包的临时目录加载
|
||||
if getattr(sys, 'frozen', False):
|
||||
alarm_file = os.path.join(sys._MEIPASS, "alarm.wav")
|
||||
else:
|
||||
alarm_file = os.path.join(os.path.dirname(__file__), "alarm.wav")
|
||||
|
||||
if os.path.exists(alarm_file):
|
||||
self.sound.setSource(QUrl.fromLocalFile(alarm_file))
|
||||
self.sound.setLoopCount(3)
|
||||
|
||||
Reference in New Issue
Block a user