O Ubuntu oferece um conjunto de bibliotecas e exemplos para usá-los para uma migração de menus simples e uma interface consistente.
Os exemplos no documento vinculado acima incluem a versão dos seguintes idiomas:
- C
- PYGI
- PYGTK
- C #
- Vala
- Haskell
Um exemplo de python da página é:
#!/usr/bin/env python
#
# Copyright 2009-2012 Canonical Ltd.
#
# Authors: Neil Jagdish Patel <[email protected]>
# Jono Bacon <[email protected]>
# David Planella <[email protected]>
#
# This program is free software: you can redistribute it and/or modify it
# under the terms of either or both of the following licenses:
#
# 1) the GNU Lesser General Public License version 3, as published by the
# Free Software Foundation; and/or
# 2) the GNU Lesser General Public License version 2.1, as published by
# the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranties of
# MERCHANTABILITY, SATISFACTORY QUALITY or FITNESS FOR A PARTICULAR
# PURPOSE. See the applicable version of the GNU Lesser General Public
# License for more details.
#
# You should have received a copy of both the GNU Lesser General Public
# License version 3 and version 2.1 along with this program. If not, see
# <http://www.gnu.org/licenses/>
#
from gi.repository import Gtk
from gi.repository import AppIndicator3 as appindicator
def menuitem_response(w, buf):
print buf
if __name__ == "__main__":
ind = appindicator.Indicator.new (
"example-simple-client",
"indicator-messages",
appindicator.IndicatorCategory.APPLICATION_STATUS)
ind.set_status (appindicator.IndicatorStatus.ACTIVE)
ind.set_attention_icon ("indicator-messages-new")
# create a menu
menu = Gtk.Menu()
# create some
for i in range(3):
buf = "Test-undermenu - %d" % i
menu_items = Gtk.MenuItem(buf)
menu.append(menu_items)
# this is where you would connect your menu item up with a function:
# menu_items.connect("activate", menuitem_response, buf)
# show the items
menu_items.show()
ind.set_menu(menu)
Gtk.main()
Você pode usar um programa da lista como um wrapper para o seu script, para que clicar no item chame seu script.
Tornar o ícone e o texto dinâmicos
(tirado de: Como posso escrever um aplicativo / indicador de painel atualizado dinamicamente? )
Este exemplo sugere o uso de GObject
. Chame gobject.threads_init()
uma inicialização de aplicativo. Em seguida, inicie seus threads normalmente, mas certifique-se de que os threads nunca realizem nenhuma tarefa GUI diretamente. Em vez disso, você usa gobject.idle_add
para agendar a tarefa da GUI diretamente.
(O acima é uma citação exata do link incluído no caso do link parar de funcionar.)
#!/usr/bin/env python3
import signal
import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk, AppIndicator3, GObject
import time
from threading import Thread
class Indicator():
def __init__(self):
self.app = 'test123'
iconpath = "/opt/abouttime/icon/indicator_icon.png"
self.indicator = AppIndicator3.Indicator.new(
self.app, iconpath,
AppIndicator3.IndicatorCategory.OTHER)
self.indicator.set_status(AppIndicator3.IndicatorStatus.ACTIVE)
self.indicator.set_menu(self.create_menu())
self.indicator.set_label("1 Monkey", self.app)
# the thread:
self.update = Thread(target=self.show_seconds)
# daemonize the thread to make the indicator stopable
self.update.setDaemon(True)
self.update.start()
def create_menu(self):
menu = Gtk.Menu()
# menu item 1
item_1 = Gtk.MenuItem('Menu item')
# item_about.connect('activate', self.about)
menu.append(item_1)
# separator
menu_sep = Gtk.SeparatorMenuItem()
menu.append(menu_sep)
# quit
item_quit = Gtk.MenuItem('Quit')
item_quit.connect('activate', self.stop)
menu.append(item_quit)
menu.show_all()
return menu
def show_seconds(self):
t = 2
while True:
time.sleep(1)
mention = str(t)+" Monkeys"
# apply the interface update using GObject.idle_add()
GObject.idle_add(
self.indicator.set_label,
mention, self.app,
priority=GObject.PRIORITY_DEFAULT
)
t += 1
def stop(self, source):
Gtk.main_quit()
Indicator()
# this is where we call GObject.threads_init()
GObject.threads_init()
signal.signal(signal.SIGINT, signal.SIG_DFL)
Gtk.main()