使用wmctrl比较窗口名的简单python循环

2024-09-29 06:31:41 发布

您现在位置:Python中文网/ 问答频道 /正文

我有点麻烦让这个代码工作。基本上,我想要的是检查wmctrl-l的输出是否有这个字符串:“Spotify-”。如果存在,请取消静音。如果它不存在,并且/或wmctrl-l包含一个名为“Spotify”的条目(行尾),那么我想静音。在

大部分代码都在工作,问题是GetWindowsPipe()只输出一行<open file '<fdopen>', mode 'rb' at 0x7f7a5f854660>。不知道怎么回事。在

有人能帮我解决这个可能很简单的问题吗?在

代码:

import subprocess, time, pdb

def GetWindowsPipe():
    pipe = subprocess.Popen("wmctrl -l", shell=True, stdout=subprocess.PIPE).stdout
    return pipe

def LowerVolume():
    subprocess.Popen("amixer -q set Master mute", shell=True)
    print 'Lowering volume'
    global volumeIsLow
    volumeIsLow = True

def RaiseVolume():
    subprocess.Popen("amixer -q set Master unmute", shell=True)
    print 'Raising volume'
    global volumeIsLow
    volumeIsLow = False

def Run():
    print 'Starting blockify.'

    global volumeIsLow    
    volumeIsLow = False
    RaiseVolume()

    while(True):

        found = False
        pipe = GetWindowsPipe()
        spot = "Spotify - "

        if spot in pipe:
            found = True
            print 'found'
            break

        if found:
            if (not volumeIsLow):
                LowerVolume()
        elif volumeIsLow:
            RaiseVolume()

        time.sleep(1)

if __name__ == "__main__":
    Run()

这里有此脚本的扩展版本:http://pastebin.com/4RtTQCtf 我只是想让它重新工作,修剪一些脂肪。在


Tags: 代码trueifdefshellglobalspotifysubprocess
1条回答
网友
1楼 · 发布于 2024-09-29 06:31:41

GetWindowsPipe()返回管道对象,它是一个类似文件的对象。为了获得文本,您需要调用pipe.read()。在

您应该替换这个:

 if spot in pipe:
    found = True
    print 'found'
    break

有了这个:

^{pr2}$

相关问题 更多 >