我正在完成一个基本上用作闹钟的python脚本,当闹钟响起时,youtube视频会打开。这是我到目前为止所做的,并且在很大程度上它完美地运作:
import time
import webbrowser
import random
import sys
def addVideo():
videoToAdd = raw_input("Enter the url of the video to add: ")
with open('alarmVideos.txt', 'a') as f:
f.write(videoToAdd + '\n')
def pickVideo():
with open('alarmVideos.txt') as file:
videosToUse = file.readlines()
useVideo = random.choice(videosToUse)
return useVideo
alarmTime = raw_input("When would you like to set your alarm to?: \nPlease use this format: 01:00 (24hr clock)\n")
localTime = time.strftime("%H:%M")
add_Video = raw_input("Would you like to add a video to your list? (y/n): \n")
if add_Video == 'y':
addVideo()
print "Your alarm is set to:", alarmTime
while alarmTime != localTime:
localTime = time.strftime("%H:%M")
time.sleep(1)
if alarmTime == localTime:
controller = webbrowser.get()
controller.open(pickVideo())
sys.exit()
但是,只有在我打开浏览器并且视频在新标签页中打开时,脚本才会结束。如果我在闹钟响起时关闭了Chrome,视频将在新窗口中打开,但脚本不会结束。我不知道为什么。在此先感谢您的帮助!