我有一个python脚本 (main.py)
,它将命令发送到另一个python脚本 (test.py)
来播放音乐和 {{1} } 播放给定目录中的音乐。这两个文件分别正常工作。但是当我将变量从(test.py)
传递给main
时,它会返回错误。 (当然,我没有正确地传递变量。但我无法弄清楚我犯了哪个错误)。
我每次都会收到以下错误:
test
我的脚本是:
main.py
music_cmd = music_command
NameError: name 'music_command' is not defined
test.py
import test
from test import playmusic
if i['tag'] == "play-music":
music_command = sentence #sentence is the input command from questions directory.
music_played = test.playmusic(soundfile)
print('playing...')
我想将问题发送到test.py并在选择了歌曲后,它会获得它的价值并播放它。
我希望我能清楚说明一切。
修改
为了让import os
import pygame
import random
from glob import glob
music_cmd = music_command
if music_cmd == 'play music':
def playmusic(soundfile):
pygame.init()
pygame.mixer.init()
clock = pygame.time.Clock()
pygame.mixer.music.load(soundfile)
pygame.mixer.music.play()
print("Playing...")
while pygame.mixer.music.get_busy():
clock.tick(1000)
def stopmusic():
"""stop currently playing music"""
pygame.mixer.music.stop()
def getmixerargs():
pygame.mixer.init()
freq, size, chan = pygame.mixer.get_init()
return freq, size, chan
def initMixer():
BUFFER = 3072 # audio buffer size, number of samples since pygame 1.8.
FREQ, SIZE, CHAN = getmixerargs()
pygame.mixer.init(FREQ, SIZE, CHAN, BUFFER)
try:
initMixer()
filenames = glob('./music/*.mp3')
playmusic(random.choice(filenames))
except KeyboardInterrupt: # to stop playing, press "ctrl-c"
stopmusic()
print ("\nPlay Stopped by user")
单独运行,我改变了这一行
test.py
到music_cmd = music_command
然后,如果条件检查music_cmd = input('Enter Comamnd')
是否具有值music_cmd
我需要这个值来自play music