Why does my notification area icon not show up when I launch my program on startup?
I'm writing a small pyGTK application that displays and updates a notification area icon. The code is as follows:
#!/usr/bin/env python2
from __future__ import division
import pygtk
pygtk.require('2.0')
import gtk
from time import time
from math import floor
gtk.gdk.threads_init()
import gobject
#Parameters
MIN_WORK_TIME = 60 * 10 # min work time in seconds
class Pomodoro:
def __init__(self):
self.icon=gtk.status_icon_new_fro开发者_JS百科m_file("idle.svg")
self.icon.set_tooltip("Idle")
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
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(state+".svg")
if state == "idle":
delta = time() - self.start_working_time
if old_state == "ok":
self.icon.set_tooltip("Good! Worked for %s." %
self.format_time(delta))
elif old_state == "working":
self.icon.set_tooltip("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.state=state
def icon_click(self,dummy):
delta = time() - self.start_working_time
if self.state == "idle":
self.set_state("working")
else:
self.set_state("idle")
def update(self):
"""This method is called everytime a tick interval occurs"""
delta = time() - self.start_working_time
if self.state == "idle":
pass
else:
self.icon.set_tooltip("Working for %s..." % self.format_time(delta))
if self.state == "working":
if delta > MIN_WORK_TIME:
self.set_state("ok")
source_id = gobject.timeout_add(self.tick_interval*1000, self.update)
def main(self):
# All PyGTK applications must have a gtk.main(). Control ends here
# and waits for an event to occur (like a key press or mouse event).
source_id = gobject.timeout_add(self.tick_interval, self.update)
gtk.main()
# If the program is run directly or passed as an argument to the python
# interpreter then create a Pomodoro instance and show it
if __name__ == "__main__":
app = Pomodoro()
app.main()
It works fine when I launch it from the terminal, but the icon isn't displayed when I launch it at the start of my graphical session (I'm using KDE, if that matters).
Any idea what could be happening? Is their a problem with my code, or is it a bug in KDE?
Are you trying to start the app from a subdirectory of your /home/username directory? I'm wondering if you need to include the full path to the image files in your code
Maybe "at the start of [your] graphical session" the notification area hasn't finished initializing. What happens if your program sleeps for a couple of seconds before displaying the icon? (I'm not saying that would be the best solution but it's quick and easy and might reveal the source of the problem)
精彩评论