Gtk 3,python,appindicator,禁用标签旁的图标

8 投票
1 回答
2661 浏览
提问于 2025-04-18 06:00

我在用Python写一个openweathermap网站的应用指示器,但我只想要指示器上的文字标签,不想要图标。但是当我把图标留空时,它却显示一个空的图标。为什么我只想要文字呢?在Ubuntu 12.04中,python-appindicator如果把图标留空就不会加载空图标,但在Ubuntu 14.04中留空就会显示空图标。怎么才能禁用图标呢?? 有什么想法吗?

#!/usr/bin/env python
# -*- coding: utf-8 -*-

import sys
import urllib2
import json 
import time
import datetime

from gi.repository import Gtk, Gdk, GLib, GObject
from gi.repository import AppIndicator3 as appindicator

class programa:

    def __init__(self):
    # Create Indicator with icon and label
        # If leave empty "" then load empty icon
        self.ind = appindicator.Indicator.new("Weather","",appindicator.IndicatorCategory.APPLICATION_STATUS)
        self.ind.set_status(appindicator.IndicatorStatus.ACTIVE) # 
        self.menu_structure()

    # Menu structure
    def menu_structure(self):
        refresh      = 720 # Refresh interval in seconds
        url          = urllib2.Request('http://api.openweathermap.org/data/2.5/weather?q=siauliai&units=metric') 
        openup       = urllib2.urlopen(url) # open url
        json_string  = openup.read() #read data
        parsed_json  = json.loads(json_string) 

        # Get data
        temp         = parsed_json['main']['temp'] # Temperature in metric system
        wind         = parsed_json['wind']['speed'] # Wind speed
        humidity     = parsed_json['main']['humidity'] 
        clouds       = parsed_json['clouds']['all']
        weather      = parsed_json['weather'][0]['main']
        weather_desc = parsed_json['weather'][0]['description']
        update_time  = parsed_json['dt']
        updated      = datetime.datetime.fromtimestamp(int(update_time)).strftime('%H:%M')
        # GTK menu
        self.menu          = Gtk.Menu()
        self.menu_updated  = Gtk.MenuItem("Updatet: "+updated)
        self.menu_weather  = Gtk.MenuItem("Weather: "+weather)
        self.menu_desc     = Gtk.MenuItem("Desc: "+weather_desc)
        self.menu_clouds   = Gtk.MenuItem("Clouds: "+str(clouds)+"%")
        self.menu_humidity = Gtk.MenuItem("Humidity: "+str(humidity)+"%")
        self.menu_wind     = Gtk.MenuItem("Wind: "+str(wind)+" m/s")
        self.separator     = Gtk.SeparatorMenuItem() 
        self.exit          = Gtk.MenuItem("Exit")
        self.exit.connect("activate", self.quit)       

        #show menu
        self.menu_updated.show()
        self.menu_weather.show()
        self.menu_desc.show()
        self.menu_clouds.show()
        self.menu_humidity.show()
        self.menu_wind.show()
        self.separator.show()
        self.exit.show()

        # Append menu
        self.menu.append(self.menu_updated)
        self.menu.append(self.menu_weather)
        self.menu.append(self.menu_desc)
        self.menu.append(self.menu_clouds)
        self.menu.append(self.menu_humidity)
        self.menu.append(self.menu_wind)
        self.menu.append(self.separator)
        self.menu.append(self.exit)

        self.ind.set_menu(self.menu)

        # Set label in celcius temperature
        self.ind.set_label(str(temp)+u"\u2103".encode('utf-8'),"")

        # close url
        openup.close()

        # Refresh indicator
        GLib.timeout_add_seconds(refresh,self.menu_structure) 

    def quit(self, widget):
        sys.exit(0)

if __name__ == "__main__":
    indicator = programa()
    Gtk.main()

1 个回答

3

我找到的唯一方法就是使用你自己设计的一个薄而透明的图标,或者使用一个现成的、比较细腻的Ubuntu图标,比如这个路径下的图标:/usr/share/unity/icons/panel-shadow.png。你也可以把这两种方法结合起来使用:

icon_image = os.path.dirname(__file__) + "/my_thin_inv_icon.png"
if not os.path.isfile(icon_image):
    icon_image = "/usr/share/unity/icons/panel-shadow.png"

self.ind = appindicator.Indicator.new("Weather",icon_image,appindicator.IndicatorCategory.APPLICATION_STATUS)

撰写回答