From f8feb5f406dd9f23d535fb987e47948cfbb5ac27 Mon Sep 17 00:00:00 2001 From: Cx330 <1487537121@qq.com> Date: Wed, 19 Mar 2025 20:00:17 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9pyinstaller=E7=BC=96=E8=AF=91?= =?UTF-8?q?=E5=90=8E=E6=89=BE=E4=B8=8D=E5=88=B0=E6=96=87=E4=BB=B6=E7=9A=84?= =?UTF-8?q?=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- musicplayer.py | 30 ++++++++++++++++++------------ 1 file changed, 18 insertions(+), 12 deletions(-) diff --git a/musicplayer.py b/musicplayer.py index 77750ce..dd2b0a6 100644 --- a/musicplayer.py +++ b/musicplayer.py @@ -66,7 +66,7 @@ class MusicPlayer(QMainWindow): self.control_layout = QHBoxLayout() self.control_layout.setAlignment(Qt.AlignmentFlag.AlignCenter) self.play_button = QPushButton() - self.play_button.setIcon(QIcon("file/play.png")) + self.play_button.setIcon(QIcon(self.resource_path("file/play.png"))) self.play_button.setIconSize(QSize(52, 52)) # 明确设置图标尺寸 self.play_button.setStyleSheet("border: none;") self.play_button.clicked.connect(self.toggle_play_pause) @@ -77,6 +77,14 @@ class MusicPlayer(QMainWindow): self.timer = QTimer(self) self.timer.timeout.connect(self.update_lyrics_and_progress) + def resource_path(self, relative_path): + """ 获取资源文件的路径,适配 PyInstaller 打包模式 """ + if getattr(sys, 'frozen', False): + base_path = sys._MEIPASS # PyInstaller 运行时解压目录 + else: + base_path = os.path.dirname(os.path.abspath(__file__)) # 开发模式 + return os.path.join(base_path, relative_path) + def set_background_color(self, color): palette = self.palette() palette.setColor(QPalette.ColorRole.Window, QColor(color)) @@ -84,7 +92,7 @@ class MusicPlayer(QMainWindow): self.central_widget.setStyleSheet(f"background-color: {color};") def load_cover(self): - cover_path = "file/cover.jpg" + cover_path = self.resource_path("file/cover.jpg") if os.path.exists(cover_path): pixmap = QPixmap(cover_path) self.cover_label.setPixmap(pixmap.scaled(256, 256, Qt.AspectRatioMode.KeepAspectRatio)) @@ -92,7 +100,7 @@ class MusicPlayer(QMainWindow): self.cover_label.setText("封面未找到") def load_lyrics(self): - lyrics_path = "file/林俊杰-光阴副本.lrc" + lyrics_path = self.resource_path("file/林俊杰-光阴副本.lrc") if os.path.exists(lyrics_path): try: with open(lyrics_path, 'r', encoding='utf-8') as file: @@ -122,7 +130,7 @@ class MusicPlayer(QMainWindow): self.pause_music() def play_music(self): - music_path = "file/林俊杰-光阴副本.wav" + music_path = self.resource_path("file/林俊杰-光阴副本.wav") if os.path.exists(music_path): try: pygame.mixer.music.load(music_path) @@ -131,29 +139,27 @@ class MusicPlayer(QMainWindow): self.start_time = time.time() self.pause_time = 0 self.timer.start(500) - self.play_button.setIcon(QIcon("file/pause.png")) + self.play_button.setIcon(QIcon(self.resource_path("file/pause.png"))) self.is_playing = True except Exception as e: - print("播放音乐时出错:", e) - else: - print("音乐文件未找到:", music_path) + pass def resume_music(self): try: pygame.mixer.music.unpause() # 直接恢复播放 self.start_time = time.time() - self.pause_time self.timer.start(500) - self.play_button.setIcon(QIcon("file/pause.png")) + self.play_button.setIcon(QIcon(self.resource_path("file/pause.png"))) self.is_playing = True except Exception as e: - print("继续播放时出错:", e) + pass def pause_music(self): if self.is_playing: self.pause_time = time.time() - self.start_time pygame.mixer.music.pause() self.timer.stop() - self.play_button.setIcon(QIcon("file/play.png")) + self.play_button.setIcon(QIcon(self.resource_path("file/play.png"))) self.is_playing = False def seek_music(self): @@ -165,7 +171,7 @@ class MusicPlayer(QMainWindow): self.current_lyric_index = self.find_lyric_index(new_time) self.update_lyrics_display() self.timer.start(500) - self.play_button.setIcon(QIcon("file/pause.png")) + self.play_button.setIcon(QIcon(self.resource_path("file/pause.png"))) self.is_playing = True def find_lyric_index(self, current_time):