15

When I'm on the train to work I connect my netbook to my Nexus One's wifi hotspot. As I go through a tunnel my phone obviously loses it's 3G connection and takes a while to re-establish once the train emerges. But the netbook wifi logo stays constant as it's still connected to the phone itself.

I've written a little python program that attempts to ping a server and thus decides if internet is available (feel free to suggest a method of detecting internet connection that would be either quicker or use less bandwidth as I am capped per month).

My question is: how can I create an applet for GNOME Panel 2.30.2 in Python, to graphically display this status, so I can decide when to continue clicking links and expecting internet to work.

I got this example with a panel button to work but would like an icon that changes depending on the situation.

I've used Python for a few years haven't but coded gnome before. I'm using the ubuntu desktop edition as my login rather than unity, on 10.04.

Tom Viner
  • 6,655
  • 7
  • 39
  • 40

2 Answers2

9

Check out this simple applet I made. It has an icon that changes depending on events. Simply replace the logic with your logic and it should do the trick. Even better, it should be compatible with all freedesktop-compatible environments.

static_rtti
  • 53,760
  • 47
  • 136
  • 192
  • 2
    Great example, thank you. Only as complex as required for the task, and no more. – Tom Viner Jun 01 '11 at 08:08
  • 2
    I've now created a github project, called [net-panel](https://github.com/tomviner/net-panel) that meets my needs. – Tom Viner Aug 16 '11 at 07:46
  • Hmm, on Ubuntu 12.04 it doesn't do anything when started. I'm just waiting for a new prompt on the command line, no icon or applet or whatever appears. Are there any update or information on what to change in order to get it running? – Alfe Mar 10 '14 at 13:49
  • 2
    I though the guidelines were to paste the code, because the links can be dead sometimes. Please paste the code in your answer, because the link is indeed dead. – Quidam May 05 '17 at 10:19
  • @Quidam The code is probably this repo https://github.com/moonranger/tomate – allprog Jun 27 '17 at 07:37
  • @allprog actually my code was a lot simpler. I'm not sure if this is based on it or not. – static_rtti Jun 28 '17 at 08:19
  • 2
    @static_rtti I used your code about two years ago. Tried to find it on my disks but it was to no avail. Sad. – allprog Jun 29 '17 at 14:19
  • The URL is dead. – Regis May Feb 10 '21 at 19:12
5

For future reference, a really nice guide on how to build indicators for Gnome3: http://candidtim.github.io/appindicator/2014/09/13/ubuntu-appindicator-step-by-step.html

Complete source code:

import signal
import json

from urllib2 import Request, urlopen, URLError

from gi.repository import Gtk as gtk
from gi.repository import AppIndicator3 as appindicator
from gi.repository import Notify as notify


APPINDICATOR_ID = 'myappindicator'

def main():
    indicator = appindicator.Indicator.new(APPINDICATOR_ID, 'sample_icon.svg', appindicator.IndicatorCategory.SYSTEM_SERVICES)
    indicator.set_status(appindicator.IndicatorStatus.ACTIVE)
    indicator.set_menu(build_menu())
    notify.init(APPINDICATOR_ID)
    gtk.main()

def build_menu():
    menu = gtk.Menu()
    item_joke = gtk.MenuItem('Joke')
    item_joke.connect('activate', joke)
    menu.append(item_joke)
    item_quit = gtk.MenuItem('Quit')
    item_quit.connect('activate', quit)
    menu.append(item_quit)
    menu.show_all()
    return menu

def fetch_joke():
    request = Request('http://api.icndb.com/jokes/random?limitTo=[nerdy]')
    response = urlopen(request)
    joke = json.loads(response.read())['value']['joke']
    return joke

def joke(_):
    notify.Notification.new("<b>Joke</b>", fetch_joke(), None).show()

def quit(_):
    notify.uninit()
    gtk.main_quit()

if __name__ == "__main__":
    signal.signal(signal.SIGINT, signal.SIG_DFL)
    main()
s3v3n
  • 8,203
  • 5
  • 42
  • 56