如何同时播放音乐和打印文字?

How to play music and print text at the same time?

我想制作这个很酷的程序,在列出我的人生目标的同时播放音乐。打印文本时我无法播放音乐。我该怎么做?

from playsound import playsound
import time

print('My life goals are:')
playsound('spongebob.mp3.mp3', 1)
print('Life goal 1 \n')
time.sleep(0.2)
print('Life goal 2 \n')
time.sleep(0.2)
print('Life goal 3 \n')
time.sleep(0.2)
print('Life goal 4')
time.sleep(0.2)
print('Life goal 5')

知道我该怎么做吗?

您可以通过创建两个线程来实现:
第一个线程将播放您喜欢的音乐。
第二个将列出您的人生目标。

from playsound import playsound
import time

from threading import Thread

def life_goal_printer():
    print('My life goals are:')
    LIFE_GOALS = ['code python' , 'eat', 'sleep' ]
    for life_goal in LIFE_GOALS: 
        print(life_goal)
        time.sleep(0.2)

def favorate_music_player():
    FAVORATE_SONG = 'spongebob.mp3.mp3'
    playsound(FAVORATE_SONG, 1)

t1 = Thread(target=life_goal_printer)
t1.start()

t2 = Thread(target=favorate_music_player)
t2.start()