修改pyinstaller编译后找不到文件的问题

This commit is contained in:
张梦南 2025-03-19 20:00:17 +08:00
parent 41622cab44
commit f8feb5f406

View File

@ -66,7 +66,7 @@ class MusicPlayer(QMainWindow):
self.control_layout = QHBoxLayout() self.control_layout = QHBoxLayout()
self.control_layout.setAlignment(Qt.AlignmentFlag.AlignCenter) self.control_layout.setAlignment(Qt.AlignmentFlag.AlignCenter)
self.play_button = QPushButton() 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.setIconSize(QSize(52, 52)) # 明确设置图标尺寸
self.play_button.setStyleSheet("border: none;") self.play_button.setStyleSheet("border: none;")
self.play_button.clicked.connect(self.toggle_play_pause) self.play_button.clicked.connect(self.toggle_play_pause)
@ -77,6 +77,14 @@ class MusicPlayer(QMainWindow):
self.timer = QTimer(self) self.timer = QTimer(self)
self.timer.timeout.connect(self.update_lyrics_and_progress) 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): def set_background_color(self, color):
palette = self.palette() palette = self.palette()
palette.setColor(QPalette.ColorRole.Window, QColor(color)) palette.setColor(QPalette.ColorRole.Window, QColor(color))
@ -84,7 +92,7 @@ class MusicPlayer(QMainWindow):
self.central_widget.setStyleSheet(f"background-color: {color};") self.central_widget.setStyleSheet(f"background-color: {color};")
def load_cover(self): def load_cover(self):
cover_path = "file/cover.jpg" cover_path = self.resource_path("file/cover.jpg")
if os.path.exists(cover_path): if os.path.exists(cover_path):
pixmap = QPixmap(cover_path) pixmap = QPixmap(cover_path)
self.cover_label.setPixmap(pixmap.scaled(256, 256, Qt.AspectRatioMode.KeepAspectRatio)) self.cover_label.setPixmap(pixmap.scaled(256, 256, Qt.AspectRatioMode.KeepAspectRatio))
@ -92,7 +100,7 @@ class MusicPlayer(QMainWindow):
self.cover_label.setText("封面未找到") self.cover_label.setText("封面未找到")
def load_lyrics(self): def load_lyrics(self):
lyrics_path = "file/林俊杰-光阴副本.lrc" lyrics_path = self.resource_path("file/林俊杰-光阴副本.lrc")
if os.path.exists(lyrics_path): if os.path.exists(lyrics_path):
try: try:
with open(lyrics_path, 'r', encoding='utf-8') as file: with open(lyrics_path, 'r', encoding='utf-8') as file:
@ -122,7 +130,7 @@ class MusicPlayer(QMainWindow):
self.pause_music() self.pause_music()
def play_music(self): def play_music(self):
music_path = "file/林俊杰-光阴副本.wav" music_path = self.resource_path("file/林俊杰-光阴副本.wav")
if os.path.exists(music_path): if os.path.exists(music_path):
try: try:
pygame.mixer.music.load(music_path) pygame.mixer.music.load(music_path)
@ -131,29 +139,27 @@ class MusicPlayer(QMainWindow):
self.start_time = time.time() self.start_time = time.time()
self.pause_time = 0 self.pause_time = 0
self.timer.start(500) 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 self.is_playing = True
except Exception as e: except Exception as e:
print("播放音乐时出错:", e) pass
else:
print("音乐文件未找到:", music_path)
def resume_music(self): def resume_music(self):
try: try:
pygame.mixer.music.unpause() # 直接恢复播放 pygame.mixer.music.unpause() # 直接恢复播放
self.start_time = time.time() - self.pause_time self.start_time = time.time() - self.pause_time
self.timer.start(500) 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 self.is_playing = True
except Exception as e: except Exception as e:
print("继续播放时出错:", e) pass
def pause_music(self): def pause_music(self):
if self.is_playing: if self.is_playing:
self.pause_time = time.time() - self.start_time self.pause_time = time.time() - self.start_time
pygame.mixer.music.pause() pygame.mixer.music.pause()
self.timer.stop() 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 self.is_playing = False
def seek_music(self): def seek_music(self):
@ -165,7 +171,7 @@ class MusicPlayer(QMainWindow):
self.current_lyric_index = self.find_lyric_index(new_time) self.current_lyric_index = self.find_lyric_index(new_time)
self.update_lyrics_display() self.update_lyrics_display()
self.timer.start(500) 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 self.is_playing = True
def find_lyric_index(self, current_time): def find_lyric_index(self, current_time):