1

我想做这个很酷的程序,在播放音乐的同时列出我的人生目标。文本打印时我无法播放音乐。我怎样才能做到这一点?

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')

知道我该怎么做吗?

4

1 回答 1

3

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

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()
于 2020-08-23T11:09:13.467 回答