2014-10-03 4 views
2

그래서 최근에 파이썬 문맹 퇴치를 테스트하는 간단한 프로젝트를 제공하기로 결정했습니다. 제가 만든 것은 누군가가 깨어있는 시간을 요구하는 알람 시계였습니다. 그리고 그 시간에 VLC로 mp3 파일을 재생하고 사용자가 무작위로 생성 된 수학 문제에 응답하면 해제됩니다. 문제는 알람 소리를 멈추게하는 방법을 알아낼 수 없다는 것입니다. killall VLC 명령을 제공하기 위해 os.popen을 사용해 보았지만 문제를 해결하지 못했습니다.재생중인 오디오 파일을 끄는 방법

#IMPORTS 
import datetime 
import time 
import os 
import sys 
import random 

#VARIABLES 
alarm_HH = 00 
alarm_MM = 00 
number_a = random.randrange(0, 999, 2) 
number_b = random.randrange(0, 999, 2) 
command_alarm = 'open -a "VLC" /Users/AlexW/Documents/alarm.mp3' 
command_VLC = 'open -a /Applications/VLC.app' 
command_close = 'killall VLC' 

#THE ACTUAL ALARM 
def alarm_function(): 
    #GLOBALS 
    global command_close 
    global command_alarm 
    global alarm_HH 
    global alarm_MM 
    global number_a 
    global number_b 
    while True: 
     now = time.localtime() 
     if now.tm_hour == int(alarm_HH) and now.tm_min == int(alarm_MM): 
      os.popen(command_alarm) 
      print ("---------------") 
      print ("Solve this math problem to disable the alarm") 
      print (number_a) 
      print ("+") 
      print (number_b) 
      print ("---------------") 
      answer = input("Enter Your Answer: ") 
      if answer == number_a + number_b: 
       os.popen(command_close) 
       print ("---------------") 
       print ("Alarm Disabled") 
       alarm_sleep() 
      else: 
       print ("---------------") 
       print("Try again") 
     else: 
      pass 

#SET THE TIME FOR THE ALARM 
def alarm_set(): 
    #GLOBALS 
    global command_VLC 
    global alarm_HH 
    global alarm_MM 
    print ("---------------") 
    alarm_HH = input("What hour do you want to wake up? (24 hour format) ") 
    print ("---------------") 
    alarm_MM = input("How about the minute? ") 
    print ("---------------") 
    print ("Opening VLC Player") 
    os.popen(command_VLC) 
    print ("---------------") 
    print ("Alarm Set") 
    print ("---------------") 
    print ("To disable the alarm, quit this program") 
    alarm_function() 

#COOLDOWN 
#Used to prevent the alarm from going off twice once the question is completed 
def alarm_sleep(): 
    time.sleep(60) 
    alarm_function() 

#STARTING SEQUENCE 
print ("----------------") 
print ("MATH ALARM CLOCK") 
print ("----------------") 
answer = input("Type <<1>> to start ") 
if answer == 1: 
    alarm_set() 
else: 
    alarm_set() 

답변

1

문제는 당신이 sudo는 권한으로 킬 (kill) 모든 명령을 실행할 필요가 있습니다 :

여기에 전체 코드입니다. VLC는 네이티브 커맨드 라인을 종료하지 않으므로 살인은 프로세스를 종료하는 올바른 방법입니다.

+0

시도해 보았지만 여전히 작동하지 않습니다. "command_close = 'sudo killall VLC'" – Alex

+1

su su를 사용하지 않고 su를 실행하고 스크립트를 재실행 할 수 있습니까? 닫기 명령의 sudo가 암호를 묻는 지 확실하지 않습니다. – TheoretiCAL