# Ubuntu – How to get the current power consumption in the status bar? (Developing a status icon)

statusbar

I wanted to ask how can I develop a simple script/app and put it in the status bar near the time (top right). Let's say I have a laptop and the script gets the battery current usage in watts every 10 seconds so it is shown in the status bar. Am using ubuntu 16 with unity

Ubuntu provides a set of libraries and examples for using them to for a migration of simple menus and a consistent interface.

The examples in the document linked above includes version is the following languages:

• C
• PYGI
• PYGTK
• C#
• Vala

A python example from the page is:

#!/usr/bin/env python
#
# Copyright 2009-2012 Canonical Ltd.
#
# Authors: Neil Jagdish Patel <neil.patel@canonical.com>
#          Jono Bacon <jono@ubuntu.com>
#          David Planella <david.planella@ubuntu.com>
#
# 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
#

from gi.repository import Gtk
from gi.repository import AppIndicator3 as appindicator

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

# create some
for i in range(3):
buf = "Test-undermenu - %d" % i

# this is where you would connect your menu item up with a function:

# show the items

Gtk.main()


You could use a program from the list as a wrapper for your script so that clicking on the item will call your script.

Making Icon and text dynamic

This example suggests using GObject. Call gobject.threads_init()an application initialization. Then launch your threads normally, but make sure the threads never do any GUI task directly. Instead, you use gobject.idle_add to schedule GUI task directly. (The above is an exact quote from the link included in case the link stops working.)

#!/usr/bin/env python3
import signal
import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk, AppIndicator3, GObject
import time

class Indicator():
def __init__(self):
self.app = 'test123'
self.indicator = AppIndicator3.Indicator.new(
self.app, iconpath,
AppIndicator3.IndicatorCategory.OTHER)
self.indicator.set_status(AppIndicator3.IndicatorStatus.ACTIVE)
self.indicator.set_label("1 Monkey", self.app)
# daemonize the thread to make the indicator stopable
self.update.setDaemon(True)
self.update.start()

# menu item 1
# separator
# quit
item_quit.connect('activate', self.stop)

def show_seconds(self):
t = 2
while True:
time.sleep(1)
mention = str(t)+" Monkeys"
# apply the interface update using  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()
`