在 ubuntu 中生成菜单

在 ubuntu 中生成菜单

在此处输入图片描述

我想更改 ubuntu 的 GUI 菜单。也就是说,我想在菜单(屏幕顶部)添加一个图标,单击该图标后将显示一个菜单,选择特定选项将运行特定命令。

有人能帮助我吗

答案1

您可以创建一个简单的 Python 小程序。下面是我拥有的一个非常简单的小程序,我已在其中标注了放置命令的位置以及命令的名称:

#!/usr/bin/env python
import sys
import gtk
import appindicator
import subprocess
import os

repo_name = "Name Of Indicator"

def sh_escape(s):
    return s.replace("\"","\\\"")

class Commands:
    def __init__(self, title="Unknown", command=""):
        self.title = title
        self.command = command

commandArr = [];
commandArr.append(Commands("Command 1 name","command one"))
commandArr.append(Commands("Command 2 name","command two"))


class RemoteApplet:
    def __init__(self):
        self.ind = appindicator.Indicator(repo_name,
                                           repo_name,
                                           appindicator.CATEGORY_APPLICATION_STATUS)
        self.ind.set_label(repo_name)
        self.ind.set_status(appindicator.STATUS_ACTIVE)
        self.ind.set_attention_icon("new-messages-red")

        self.menu_setup()
        self.ind.set_menu(self.menu)

    def menu_setup(self):
        self.menu = gtk.Menu()
        self.command_items = []
        cnt = 0
        for x in commandArr:
            self.command_items.append(gtk.MenuItem(x.title))
            self.command_items[-1].connect("activate", self.handleitem, cnt)
            self.command_items[-1].show()
            self.menu.append(self.command_items[-1])
            cnt += 1
        self.quit_item = gtk.MenuItem("Quit")
        self.quit_item.connect("activate", self.quit)
        self.quit_item.show()
        self.menu.append(self.quit_item)

    def main(self):
        self.login()
        print "Started!"
        gtk.main()

    def quit(self, widget):
        sys.exit(0)

    def login(self):
        pass

    def handleitem(self, widget, index):
        print "Running... " + commandArr[index].command
        proc = subprocess.Popen(commandArr[index].command,stdout=subprocess.PIPE, shell=True)
        (out, err) = proc.communicate()
        os.system("/usr/bin/notify-send \"" + sh_escape(repo_name + " - Output") + "\" \"" + sh_escape(out) + "\"")


if __name__ == "__main__":
    print "Starting..."
    indicator = RemoteApplet()
    indicator.main()

相关内容