Python AppIndicator for Unity Compatibility

I just opened tomate: https://gitorious.org/tomate is a very simple program that helps to achieve results when you are working on a computer.

But use gtk.status_icon instead of appindicator instead, so I would like to fix this.

However, I would like to follow the same behavior with the appindicator. It is very easy to use, like this, and I do not want to create a menu and complicate its use.

The simple behavior is that when you click on the icon, you start the time counter, and when you click again, you stop it.

Therefore, you do not need a menu.

Is it possible to use the appindicator without a menu or with a basic menu that just receives a click and does not have any elements?

Thanks for the help.

+5
source share
1 answer

In fact, menus are what make appindicator so different.

So, I just created a menu and changed the way the menu and icon are updated.

diff -r 13c201b1030a tomate.py
--- a/tomate.py Fri May 13 19:48:31 2011 +0200
+++ b/tomate.py Wed May 18 09:26:43 2011 +0200
@@ -1,53 +1,79 @@
 #!/usr/bin/env python
 from __future__ import division

 import pygtk
 pygtk.require('2.0')
 import gtk
 import os
 from time import time
 from math import floor
 gtk.gdk.threads_init()
 import gobject
+import appindicator

 #Parameters
 MIN_WORK_TIME = 60 * 10 # min work time in seconds

 class Pomodoro:
     def __init__(self):
-        self.icon=gtk.status_icon_new_from_file(self.icon_directory()+"idle.png")
-        self.icon.set_tooltip("Idle")
+        self.ind = appindicator.Indicator("tomate","tomate", 
+                                       appindicator.CATEGORY_APPLICATION_STATUS)
+
+        self.ind.set_status (appindicator.STATUS_ACTIVE)
+        self.ind.set_icon(self.icon_directory()+"idle.png")
         self.state = "idle"
         self.tick_interval=10 #number of seconds between each poll
-        self.icon.connect('activate',self.icon_click)
-        self.icon.set_visible(True)
         self.start_working_time = 0
+        self.menu = gtk.Menu()
+        # Tooltip item
+        self.item = gtk.MenuItem('Idle')
+        self.item.connect("activate", self.icon_click, None)
+        self.item.show()
+        self.menu.append(self.item)
+        # A separator
+        separator = gtk.SeparatorMenuItem()
+        separator.show()
+        self.menu.append(separator)
+        # A quit item
+        item = gtk.MenuItem('Quit')
+        item.connect("activate", gtk.main_quit, None)
+        item.show()
+        self.menu.append(item)
+        self.menu.show_all()
+        self.ind.set_menu(self.menu)
     def format_time(self,seconds):
         minutes = floor(seconds / 60)
         if minutes > 1:
             return "%d minutes" % minutes
          else:
-            return "%d minute" % minutes
     def set_state(self,state):
         old_state=self.state
-        self.icon.set_from_file(self.icon_directory()+state+".png")
+        self.ind.set_icon(self.icon_directory()+state+".png")
         if state == "idle":
             delta = time() - self.start_working_time
             if old_state == "ok":
-                self.icon.set_tooltip("Good! Worked for %s." % 
+                self.item.get_child().set_text("Good! Worked for %s." % 
                         self.format_time(delta))
             elif old_state == "working":
-                self.icon.set_tooltip("Not good: worked for only %s." % 
+                self.item.get_child().set_text("Not good: worked for only %s." % 
                         self.format_time(delta))
         else:
             if state == "working":
                 self.start_working_time = time()
             delta = time() - self.start_working_time
-            self.icon.set_tooltip("Working for %s..." % self.format_time(delta))
+            self.item.get_child().set_text("Working for %s..." % self.format_time(delta))
         self.state=state
     def icon_directory(self):
         return os.path.dirname(os.path.realpath(__file__)) + os.path.sep
-    def icon_click(self,dummy):
+    def icon_click(self, *args):
         delta = time() - self.start_working_time
         if self.state == "idle":
             self.set_state("working")
@@ -59,7 +85,7 @@
         if self.state == "idle":
             pass
         else:
-            self.icon.set_tooltip("Working for %s..." % self.format_time(delta))
+            self.item.get_child().set_text("Working for %s..." % self.format_time(delta))
             if self.state == "working":
                 if delta > MIN_WORK_TIME:
                     self.set_state("ok")
+5
source

All Articles