python - 为什么在启动程序时我的通知区域图标不显示?

标签 python linux gtk notifications pygtk

我正在编写一个显示和更新通知区域图标的小型 pyGTK 应用程序。代码如下:

#!/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_from_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()

当我从终端启动它时它工作正常,但是当我在我的图形 session 开始时启动它时没有显示图标(如果这很重要,我正在使用 KDE)。

知道会发生什么吗?他们是我的代码有问题,还是 KDE 中的错误?

最佳答案

您是否尝试从/home/username 目录的子目录启动应用程序?我想知道您是否需要在代码中包含图像文件的完整路径

关于python - 为什么在启动程序时我的通知区域图标不显示?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5770663/

相关文章:

c++ - OpenCV - 如何使用 Eclipse C++ IDE 将在/home 中编译的 opencv 库优先于来自/usr/lib/的那些

reference - 未定义对 `gtk_menu_bar_append'的引用

python - TypeError : super() argument 1 must be type, 不是 MagicMock - azure-devops python 包

python - GitPython:获取尚未应用的远程提交列表

python - 如何根据多个因素拆分一行中的项目(python)

Python 闭包和单元格(闭包值)

python - 如何让 python logging.debug() 消息输出到标准输出?

linux - 如何在服务器上为使用 docker 的指定应用程序查找 css/js 文件

c++ - 如何在 windows api 中使用匿名管道(并传递给 gtk 函数)?

c - gtk-back 按钮不会从网格中删除,列表存储 TreeView 行未显示完整