AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / ubuntu / 问题 / 490634
Accepted
Bernmeister
Bernmeister
Asked: 2014-07-02 21:09:39 +0800 CST2014-07-02 21:09:39 +0800 CST 2014-07-02 21:09:39 +0800 CST

应用程序指示器图标在单击之前不会更改

  • 772

我有一个应用程序指示器,它将月相/方向显示为其图标(以及文本标签)。当指示器进行更新时(每小时几次),图标会重新创建(作为 SVG 文件),因此图标会随时间变化。

最近,图标没有显示任何变化,除非我用鼠标单击图标(或标签)。我在 Ubuntu 14.04 64 位上,我的指标使用 Python 3 和 Appindicator3。

下面的代码代表了这个问题,并显示了一个应该每三秒更改一次的图标(我将间隔设置为三秒以快速查看问题 - 实际上图标最多每小时更改一次)。图标本身是一个 SVG 文件,并显示一个数字计数,从零开始,每三秒递增一次。

我已经在 Ubuntu 12.04 和 Xubuntu 12.04(通过 VirtualBox)上进行了测试,图标每三秒更新一次就会改变。

在 Ubuntu 14.04 上测试,图标不会改变,除非我点击它。如果我在每次更新时更改标签文本,图标也会更改,但前提是我不为标签重复相同的文本。

有人能证实这一点吗?我找不到错误报告,并且 Python AppIndicator 的 API 不再可用,因此我无法判断是否有某些内容已更改或已弃用。

#!/usr/bin/env python3


try: from gi.repository import AppIndicator3
except: pass

from gi.repository import GLib, Gtk

import os


class IndicatorTestIcon:

    NAME = "indicator-test-icon"
    SVG_ICON = "." + NAME
    SVG_FILE = os.getenv( "HOME" ) + "/" + SVG_ICON + ".svg"


    def __init__( self ):
        self.count = 0

        self.indicator = AppIndicator3.Indicator.new( IndicatorTestIcon.NAME, "", AppIndicator3.IndicatorCategory.APPLICATION_STATUS )
        self.indicator.set_icon_theme_path( os.getenv( "HOME" ) )
        self.indicator.set_status( AppIndicator3.IndicatorStatus.ACTIVE )


    def main( self ):
        self.update()
        GLib.timeout_add_seconds( 3, self.update )
        Gtk.main()


    def update( self ):
        self.buildMenu()

        self.createIcon()

        self.indicator.set_icon( IndicatorTestIcon.SVG_ICON )

        #        self.indicator.set_label( "static label", "" ) # Using a static label, the icon does not change unless clicked with the mouse.
        self.indicator.set_label( str( self.count ), "" ) # Using a dynamic label (which does not repeat) DOES change the icon.

        self.count += 1

        return True


    def buildMenu( self ):
        menu = Gtk.Menu()

        quitMenuItem = Gtk.ImageMenuItem.new_from_stock( Gtk.STOCK_QUIT, None )
        quitMenuItem.connect( "activate", Gtk.main_quit )
        menu.append( quitMenuItem )

        self.indicator.set_menu( menu )
        menu.show_all()


    def createIcon( self ):
        header = '<?xml version="1.0" standalone="no"?>' \
            '<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">' \
            '<svg xmlns="http://www.w3.org/2000/svg" version="1.1" viewBox="0 0 100 100">'

        text = '<g>' \
            '<text x="0" y="75" font-family="Verdana" font-size="100" fill="white" >' + str( self.count ) + '</text>' \
            '</g>'

        footer = '</svg>'

        with open( IndicatorTestIcon.SVG_FILE, "w" ) as f:
            f.write( header + text + footer )
            f.close()


if __name__ == "__main__": IndicatorTestIcon().main()

仅供参考:提交了错误报告https://bugs.launchpad.net/ubuntu/+bug/1337620

python
  • 2 2 个回答
  • 1726 Views

2 个回答

  • Voted
  1. user.dz
    2014-07-04T20:51:35+08:002014-07-04T20:51:35+08:00

    我认为这是合理的,因为主题路径和图标名称没有改变,因此不会更新/重新加载指示器图标(为什么它应该减少一些周期来重新加载它?这是最佳的,不是吗?!)。

    好吧,libappindicator专为从文件加载的静态图标而设计。(lp bug#812067:需要 API:pixbuf 图标设置支持)。您想通过更改图标文件内容来解决这个问题。在你的情况下没问题,但不适用于需要更短更新延迟(例如:120 毫秒)的一般用途,即太多的文件 io 请求(打开、写入、关闭)。

    • 或者,你改变主题路径这是一种技巧:

      self.indicator.set_icon_theme_path( os.getenv( "HOME" )+"./"*(self.count % 2) )
      self.indicator.set_icon_full( IndicatorTestIcon.SVG_ICON, str( self.count ) )
      
    • 或更改名称(您可以使用与之前相同的方法,在 2 个名称之间交替):

      SVG_ICON = "." + NAME+ "_{0}"
      ...
      self.indicator.set_icon_full( IndicatorTestIcon.SVG_ICON.format(self.count % 2) , str( self.count ) )
      ...
      with open( IndicatorTestIcon.SVG_FILE.format(self.count % 2), "w" ) as f:
      

    顺便说一句,最好使用set_icon_full,set_icon已弃用。(在 Ubuntu 14.04 上测试)

    • 1
  2. Best Answer
    Bernmeister
    2014-07-07T00:14:03+08:002014-07-07T00:14:03+08:00

    在测试了各种场景之后,最简单的解决方法(除了假设它是一个错误而修复错误本身之外)是来回交替图标文件的命名......

    #!/usr/bin/env python3
    
    
    from gi.repository import AppIndicator3, GLib, Gtk
    
    import glob, os
    
    
    class IndicatorTestIcon:
    
        NAME = "indicator-test-icon"
        ICON_STATE = True
    
    
        def __init__( self ):
            self.count = 0
    
            for file in glob.glob( os.getenv( "HOME" ) + "/." + IndicatorTestIcon.NAME + "*.svg" ):
                print(file)
                os.remove( file )
    
            self.indicator = AppIndicator3.Indicator.new( IndicatorTestIcon.NAME, "", AppIndicator3.IndicatorCategory.APPLICATION_STATUS )
            self.indicator.set_icon_theme_path( os.getenv( "HOME" ) )
            self.indicator.set_status( AppIndicator3.IndicatorStatus.ACTIVE )
    
    
        def main( self ):
            self.update()
            GLib.timeout_add_seconds( 3, self.update )
            Gtk.main()
    
    
        def update( self ):
    
            print( self.count )
            self.buildMenu()
    
            self.createIcon()
    
            self.indicator.set_label( "static label", "" )
            self.indicator.set_icon_full( self.getIconName(), "" )
    
            self.count += 1
    
            self.toggleIconState()
            return True
    
    
        def buildMenu( self ):
            menu = Gtk.Menu()
    
            quitMenuItem = Gtk.ImageMenuItem.new_from_stock( Gtk.STOCK_QUIT, None )
            quitMenuItem.connect( "activate", Gtk.main_quit )
            menu.append( quitMenuItem )
    
            self.indicator.set_menu( menu )
            menu.show_all()
    
    
        def createIcon( self ):
            header = '<?xml version="1.0" standalone="no"?>' \
                '<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">' \
                '<svg xmlns="http://www.w3.org/2000/svg" version="1.1" viewBox="0 0 100 100">'
    
            text = '<g>' \
                '<text x="0" y="75" font-family="Verdana" font-size="100" fill="white" >' + str( self.count ) + '</text>' \
                '</g>'
    
            footer = '</svg>'
    
            with open( self.getIconFile(), "w" ) as f:
                f.write( header + text + footer )
                f.close()
    
    
        def getIconName( self ):
            if IndicatorTestIcon.ICON_STATE: return "." + IndicatorTestIcon.NAME + "-1"
    
            return "." + IndicatorTestIcon.NAME + "-2"
    
    
        def getIconFile( self ):
            if IndicatorTestIcon.ICON_STATE: return os.getenv( "HOME" ) + "/." + IndicatorTestIcon.NAME + "-1.svg"
    
            return os.getenv( "HOME" ) + "/." + IndicatorTestIcon.NAME + "-2.svg"
    
    
        def toggleIconState( self ): IndicatorTestIcon.ICON_STATE = not IndicatorTestIcon.ICON_STATE
    
    
    if __name__ == "__main__": IndicatorTestIcon().main()
    
    • 0

相关问题

  • 默认的字符编码是什么?

  • 如何使用 pynotify 创建可点击通知?

  • 有没有安装 Django 1.2.*(最新稳定版)的简单方法?

  • 为 Python 应用程序设置构建系统

  • 为我的 PPA 创建包时遇到问题

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    如何运行 .sh 脚本?

    • 16 个回答
  • Marko Smith

    如何安装 .tar.gz(或 .tar.bz2)文件?

    • 14 个回答
  • Marko Smith

    如何列出所有已安装的软件包

    • 24 个回答
  • Marko Smith

    无法锁定管理目录 (/var/lib/dpkg/) 是另一个进程在使用它吗?

    • 25 个回答
  • Martin Hope
    Flimm 如何在没有 sudo 的情况下使用 docker? 2014-06-07 00:17:43 +0800 CST
  • Martin Hope
    Ivan 如何列出所有已安装的软件包 2010-12-17 18:08:49 +0800 CST
  • Martin Hope
    La Ode Adam Saputra 无法锁定管理目录 (/var/lib/dpkg/) 是另一个进程在使用它吗? 2010-11-30 18:12:48 +0800 CST
  • Martin Hope
    David Barry 如何从命令行确定目录(文件夹)的总大小? 2010-08-06 10:20:23 +0800 CST
  • Martin Hope
    jfoucher “以下软件包已被保留:”为什么以及如何解决? 2010-08-01 13:59:22 +0800 CST
  • Martin Hope
    David Ashford 如何删除 PPA? 2010-07-30 01:09:42 +0800 CST

热门标签

10.10 10.04 gnome networking server command-line package-management software-recommendation sound xorg

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve