python - 从if [Python]退出

标签 python audio amixer

如果构造我不能退出

#!/usr/bin/python2.7
import os
import re
import time
global state


#print status_on.group()
def main():
    get_current_jack_status = """amixer -c1 contents | grep -A 2 'Headphone Jack'"""
    output = os.popen(get_current_jack_status).read()
    status_on = re.search('values=on', output)
    if status_on:
        state = status_on
        os.system("notify-send 'Audio Manager' 'An audio jack was plugged in' -i audacity.png")
        if status_on == state:
            print "True"
            state = 1   #here i can't exit

        if status_on != state:
            print state
            os.system("notify-send 'Audio Manager' 'An audio jack was unplugged' -i audacity.png")


while True:
    main()

我尝试通过,但是当我执行脚本时,它显示为输出:
True
1
True
1


如果我使用“break”,它将崩溃。

最佳答案

这应该工作。将全局声明移到main函数内部,然后这将初始化状态,然后检查状态更改。这可能可以清除一点(即if语句),但它应该可以工作。

#!/usr/bin/python2.7
import os
import sys
import re
import time


#print status_on.group()
def main():
    global state
    get_current_jack_status = """amixer -c1 contents | grep -A 2 'Headphone Jack'"""
    output = os.popen(get_current_jack_status).read()
    status_on = re.search('values=on', output)
    if (status_on is None and state) or (state is None and status_on):
        state = status_on
        if status_on:
            print "a jack was plugged in"
            os.system("notify-send 'Audio Manager' 'An audio jack was plugged in' -i audacity.png")
        else:
            print "a jack was unplugged"
            os.system("notify-send 'Audio Manager' 'An audio jack was unplugged' -i audacity.png")

get_current_jack_status = """amixer -c1 contents | grep -A 2 'Headphone Jack'"""
output = os.popen(get_current_jack_status).read()
state = re.search('values=on', output)

while True:
        main()

关于python - 从if [Python]退出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41473851/

相关文章:

c - 如何使用 alsa 库 API 与耳机和扬声器一起使用?

html - NodeJS HTML5 音频播放器 MPEG 文件

javascript - 有关构建基于浏览器的多达32个音轨的混音器的建议

python - 如何处理专有 Python 包名称与公共(public)包名称冲突?

python - 重新采样 MultiIndexed Pandas DataFrame 并将不同的函数应用于列

javascript - 如何将我的 javascript 与我的网页合并?

php - 使用 php exec 和 amixer 设置音量

python - Coinbase send_money API 总是返回 "amount is below the current minimum"错误

python - 在 Python 中迭代两个字典并获取一个列表