如何才能让其他人(和我自己)可以打开 .py 文件,并且在代码中使用 mp3 文件时不会使其崩溃?

问题描述 投票:0回答:1
我对编程还很陌生,到目前为止我已经做了一些项目,但这是我第一次在程序中使用文件,这些文件不仅仅是使用代码自动创建的txt文件(使用open.. .)

该程序是一个闹钟。我有一个播放闹钟声音的 mp3 文件,并且我在代码中提供了它的绝对路径。它在 PyCharm 中完美运行,但当尝试启动 .py 文件时,它会立即崩溃。我已经打开了默认目录中的文件,mp3 文件也位于该目录中,但它仍然崩溃。

import time import os os.environ['PYGAME_HIDE_SUPPORT_PROMPT'] = "hide" import pygame def set_alarm(): """Function to set the alarm""" while True: try: print("Set the alarm time:") hours = int(input("Enter the hour for the alarm to ring (0-23): ")) if hours < 0 or hours > 23: # if hours are below 0 and higher than 23 print("Invalid hour entered. Please choose between hour 0 and 23.") else: # if hours are between 0 and 23 while True: # start another loop try: minutes = int(input("Enter the minutes for the alarm to ring (0-59): ")) if minutes < 0 or minutes > 59: # if minutes are below 0 and higher than 59 print("Invalid minute entered. Please choose between minute 0 and 59.") else: # if minutes are between 0 and 59 return hours, minutes # returns hours and minutes value except ValueError: print("Please enter a number between 0 and 59.") except ValueError: print("Please enter a number between 0 and 23.") def check_time(): """Function to get the current time""" current_time = time.localtime() # current_time variable assigned to the localtime command which retrieves local time return current_time.tm_hour, current_time.tm_min # .tm_hour retrieves current hour and .tm_min retrieves current minute def trigger_alarm(alarm_name, alarm_message, alarm_sound): """Function to trigger the alarm""" alarm_sound.play() # plays the audio file assigned to alarm_sound variable print(f"{alarm_name} - {alarm_message} is ringing!") while True: stop_alarm = input("Press Enter to stop the alarm: ") if not stop_alarm: # if stop_alarm input is empty (Enter is pressed) alarm_sound.stop() # audio file stops playing break # while True loop starts again if condition is not met def snooze_alarm(alarm_name, alarm_message, alarm_sound): """Function to snooze the alarm""" while True: try: snooze = input("Do you want to snooze the alarm? (yes/no): ") if snooze.lower() == "yes": try: snooze_minutes = int(input("Enter how long to snooze the alarm for (in minutes): ")) print( f"Alarm will ring again in {snooze_minutes} minute/s.") # prints in how many minutes the alarm will ring again (initial input) snooze_length_seconds = snooze_minutes * 60 # snooze_length_seconds variable assigned to the "minutes" (seconds) entered and multiplies it by 60 time.sleep(snooze_length_seconds) # sleeps for that many seconds trigger_alarm(alarm_name, alarm_message, alarm_sound) # triggers the alarm after the sleep except ValueError: print("Please enter a number.") else: # if user inputs anything other than "yes" print("You chose not to snooze the alarm.") print("=======================") break # break the snooze alarm loop except ValueError: print("Please choose yes or no.") def main(): print("Loading...") pygame.mixer.init() # initializes pygame mixer module alarm_sound_path = r"C:\Users\Marko\Documents\python projects\files\mp3\alarm sound.mp3" # alarm sound location specified, absolute path if not os.path.exists(alarm_sound_path): # if the path does not exist print("Alarm sound file not found.") return alarm_sound = pygame.mixer.Sound(alarm_sound_path) # alarm sound variable assigned to mp3 file which is played using .Sound alarm_hours, alarm_minutes = set_alarm() # double variable used, one assigned to hour, one assigned to minute, in order of which they are retrieved alarm_name = input("Enter the alarm name: ") alarm_message = input("Enter the alarm message: ") alarm_set = True # alarm_set set to True indicating that the alarm is enabled initially if set by user print(f"Alarm will ring at {alarm_hours}:{alarm_minutes}") print("====================================") while True: current_hour, current_minute = check_time() # checks time in a while loop (continuously) if alarm_set and current_hour == alarm_hours and current_minute == alarm_minutes: # if the set time is the same as the hours and minute of current time trigger_alarm(alarm_name, alarm_message, alarm_sound) # alarm is triggered snooze_alarm(alarm_name, alarm_message, alarm_sound) # snoozes the alarm if user requests, else skips this line alarm_set = False # disables alarm after triggering another_alarm = input("Do you want to set another alarm? (yes/no): ") if another_alarm.lower() == "yes": # if user inputs "yes" main() # calls the main function again else: print("Exiting.") break # breaks entire code loop time.sleep(1) # checks time every 1 second if __name__ == "__main__": main()
`Traceback (most recent call last): File "C:\Users\Marko\PycharmProjects\alarm_clock\alarm_clock.py", line 4, in <module> import pygame ModuleNotFoundError: No module named 'pygame'`
我不介意发送与 mp3 文件捆绑在一起的 .py 文件,至少在一开始是这样,但如果有一种方法不必这样做,而且也不太复杂,我不介意对此做出解释任何一个。我不喜欢太快地进入太复杂的事情,我更喜欢在学习过程中慢慢来,但我仍然不介意。

我在网上查看了一些我可以做的事情的资源,并且我尝试过做其中的一些,但似乎都不起作用,我肯定只是做错了一些事情。我看到了一些关于

base64 的事情,但我认为我不太理解它。如果这是一个有效的选项,请告诉我如何做到这一点。

python pycharm base64 mp3 alarm
1个回答
0
投票

Pycharm 可能使用包含 pygame 依赖项的虚拟环境

,而这似乎并不存在于您的默认 Python 安装中。也许它正在使用“虚拟环境”(这就是您最初设置项目的方式)。如果您在 shell 中加载 Pycharm 使用的相同虚拟环境,它应该可以工作。
关于将 MP3 与它捆绑在一起,您可以简单地使用相对路径并将其与 Python 脚本一起分发,或者您可以使用 pyinstaller

之类的东西来制作带有捆绑资源的可执行文件。这还有一个额外的优点,即运行它的人不需要安装 Python 以及程序的依赖项。

© www.soinside.com 2019 - 2024. All rights reserved.