Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/jpa/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 更改appindicator';s图标在其他进程完成后出现_Python_Appindicator - Fatal编程技术网

Python 更改appindicator';s图标在其他进程完成后出现

Python 更改appindicator';s图标在其他进程完成后出现,python,appindicator,Python,Appindicator,我正在为写一个appindicator,一个同步Google驱动器文件的守护进程。由于我的编程经验很少,所以我决定编写一个Python脚本,它将GrIVE称为子过程,而不是将其集成到C++源代码中。 我对Stefaan Lippens的功能进行了调整,以便在发生重要事件(例如,添加新文件或网络错误)时显示通知并更改指示器图标。通知效果良好;但是,指示器的图标只有在整个过程完成后才会更改,这是没有用的,因为在完成后我需要多次更改它 以下是我正在使用的代码: async.py #!/usr/bin/

我正在为写一个appindicator,一个同步Google驱动器文件的守护进程。由于我的编程经验很少,所以我决定编写一个Python脚本,它将GrIVE称为子过程,而不是将其集成到C++源代码中。 我对Stefaan Lippens的功能进行了调整,以便在发生重要事件(例如,添加新文件或网络错误)时显示通知并更改指示器图标。通知效果良好;但是,指示器的图标只有在整个过程完成后才会更改,这是没有用的,因为在完成后我需要多次更改它

以下是我正在使用的代码:

async.py

#!/usr/bin/python
# -*- coding: utf-8 -*-
import subprocess
import time
import threading
import Queue

class AsynchronousFileReader(threading.Thread):
    '''
    Helper class to implement asynchronous reading of a file
    in a separate thread. Pushes read lines on a queue to
    be consumed in another thread.
    '''

    def __init__(self, fd, queue):
        assert isinstance(queue, Queue.Queue)
        assert callable(fd.readline)
        threading.Thread.__init__(self)
        self._fd = fd
        self._queue = queue

    def run(self):
        '''The body of the tread: read lines and put them on the queue.'''
        for line in iter(self._fd.readline, ''):
            self._queue.put(line)

    def eof(self):
        '''Check whether there is no more content to expect.'''
        return not self.is_alive() and self._queue.empty()

def run(command, show):
    '''
    Main function to consume the output of a command.
    command = The command to be run
    show = Function that will process output
    '''
    # Launch the command as subprocess.
    process = subprocess.Popen(command, shell=True, stdin=None, stdout=subprocess.PIPE, stderr=subprocess.PIPE, close_fds=True)

    # Launch the asynchronous readers of the process' stdout and stderr.
    stdout_queue = Queue.Queue()
    stdout_reader = AsynchronousFileReader(process.stdout, stdout_queue)
    stdout_reader.start()
    stderr_queue = Queue.Queue()
    stderr_reader = AsynchronousFileReader(process.stderr, stderr_queue)
    stderr_reader.start()

    # Check the queues if we received some output (until there is nothing more to get).
    while not stdout_reader.eof() or not stderr_reader.eof():
        # Show what we received from standard output.
        while not stdout_queue.empty():
            line = stdout_queue.get()
            show(line)

        # Show what we received from standard error.
        while not stderr_queue.empty():
            line = stderr_queue.get()
            show(line)

        # Sleep a bit before asking the readers again.
        time.sleep(.1)

    # Let's be tidy and join the threads we've started.
    stdout_reader.join()
    stderr_reader.join()

    # Close subprocess' file descriptors.
    process.stdout.close()
    process.stderr.close()

    return True
格里夫·皮

#!/usr/bin/python
# -*- coding: utf-8 -*-
import subprocess
import time
import async
from gi.repository import Gtk
from gi.repository import Gio
from gi.repository import GObject
from gi.repository import Notify
from gi.repository import AppIndicator3 as AppIndicator

GRIVE_FOLDER = "/home/ilhuitemoc/Publike/Google Drive"

def show(line):
    line = line.replace("\n", "")
    print line
    if line.startswith("[gr::expt::MsgTag*] = "):
        line = line.replace("[gr::expt::MsgTag*] = ","",1)
        n = Notify.Notification.new("Error", line, icon)
        n.show()
        indicator.set_icon("ubuntuone-client-offline")
    if line.startswith("sync "):
        line = line.replace("sync ","",1)
        line = line.replace('"','<b>',1)
        line = line.replace('"','</b>',1)
        n = Notify.Notification.new("Sync in progress", line, icon)
        n.show()
        indicator.set_icon("ubuntuone-client-updating")
    if "Finished!" in line:
        #n = Notify.Notification.new(line, line, icon)
        #n.show()
        indicator.set_icon("ubuntuone-client-idle")

def openinfolder(obj):
    subprocess.call(["xdg-open",GRIVE_FOLDER])

def openinbrowser(obj):
    subprocess.call(["xdg-open","http://drive.google.com/"])

if __name__ == '__main__':
    subprocess.call(["killall","pantheon-notify"])
    time.sleep(1)

    indicator = AppIndicator.Indicator.new('grive', 'ubuntuone-client-offline', AppIndicator.IndicatorCategory.APPLICATION_STATUS)
    indicator.set_status(AppIndicator.IndicatorStatus.ACTIVE)
    menu = Gtk.Menu()

    status = Gtk.MenuItem("Connecting...") #Not finished yet
    status.set_sensitive(False)
    menu.append(status)

    sp = Gtk.SeparatorMenuItem()
    menu.append(sp)

    mi = Gtk.MenuItem("Open Google Drive folder")
    mi.connect('activate',openinfolder)
    menu.append(mi)

    mi = Gtk.MenuItem("Go to Google Drive webpage")
    mi.connect('activate',openinbrowser)
    menu.append(mi)

    sp = Gtk.SeparatorMenuItem()
    menu.append(sp)

    mi = Gtk.ImageMenuItem("Quit")
    img = Gtk.Image.new_from_stock(Gtk.STOCK_QUIT, Gtk.IconSize.MENU)
    mi.set_image(img)
    mi.connect('activate',Gtk.main_quit)
    menu.append(mi)

    menu.show_all()

    indicator.set_menu(menu)

    Notify.init('grive')
    icon = 'google-drive'

    #async.run('cd "%s" && grive' % GRIVE_FOLDER, show)
    GObject.timeout_add(5*60000, async.run, 'cd "%s" && grive' % GRIVE_FOLDER, show)
    #GObject.timeout_add(5000, async.run, "test.sh", show)
    Gtk.main()
#/usr/bin/python
#-*-编码:utf-8-*-
导入子流程
导入时间
异步导入
从gi.repository导入Gtk
从gi.repository导入Gio
从gi.repository导入GObject
从gi.repository导入通知
从gi.repository导入AppIndicator3作为AppIndicator
GRIVE_FOLDER=“/home/ilhuitemoc/Publike/Google Drive”
def显示(行):
行=行。替换(“\n”和“”)
打印行
如果line.startswith(“[gr::expt::MsgTag*]=”):
line=line.replace(“[gr::expt::MsgTag*]=”,“”,1)
n=Notify.Notification.new(“错误”、行、图标)
n、 show()
指示器。设置图标(“ubuntuone客户端脱机”)
如果line.startswith(“同步”):
行=行。替换(“同步”,“1”)
行=行。替换(“,”,1)
行=行。替换(“,”,1)
n=Notify.Notification.new(“正在同步”,行,图标)
n、 show()
指示器。设置图标(“ubuntuone客户端更新”)
如果行中有“完成!”:
#n=Notify.Notification.new(行、行、图标)
#n、 show()
指示器。设置图标(“ubuntuone客户端空闲”)
def openinfolder(obj):
子进程调用([“xdg打开”,GRIVE_文件夹])
def openinbrowser(obj):
子进程调用([“xdg open”http://drive.google.com/"])
如果uuuu name uuuuuu='\uuuuuuu main\uuuuuuu':
子进程调用([“killall”,“pantheon notify”])
时间。睡眠(1)
indicator=AppIndicator.indicator.new('grive','ubuntuone client offline',AppIndicator.indicator.category.APPLICATION_STATUS)
指示器。设置_状态(应用指示器。指示器状态。激活)
menu=Gtk.menu()
status=Gtk.MenuItem(“连接…”)#尚未完成
状态。设置为敏感(错误)
菜单.附加(状态)
sp=Gtk.separatoremunitem()
菜单.附加(sp)
mi=Gtk.MenuItem(“打开谷歌硬盘文件夹”)
mi.connect('activate',openinfolder)
菜单追加(mi)
mi=Gtk.MenuItem(“转到谷歌硬盘网页”)
mi.connect('activate',openinbrowser)
菜单追加(mi)
sp=Gtk.separatoremunitem()
菜单.附加(sp)
mi=Gtk.ImageMenuItem(“退出”)
img=Gtk.Image.new_from_stock(Gtk.stock_退出,Gtk.IconSize.MENU)
mi.设置图像(img)
mi.connect('激活',Gtk.main_退出)
菜单追加(mi)
菜单。全部显示()
指示器。设置菜单(菜单)
Notify.init('grive')
图标='谷歌硬盘'
#异步运行('cd“%s”和&grive“%grive\u文件夹,显示)
GObject.timeout\u add(5*60000,async.run,'cd“%s”和&grive“%grive\u文件夹,显示)
#GObject.timeout\u add(5000,async.run,“test.sh”,show)
Gtk.main()
我想我做错了什么,但这对我来说并不明显。使用子流程修改指标是否正确?或者我有没有其他方法可以正确地做到这一点