How can I display text on a StatusIcon instead of setting an icon?

随声附和 提交于 2019-12-12 09:40:48

问题


I'm a beginner developer in Gtk and also in Python. I'm trying to create a gtk.StatusIcon displaying a text string in place of a icon. How can I accomplish this with PixBuf, or any other way?

Thanks.


回答1:


This is possible - two ideas come to mind:

a.) Use an offscreen GtkLabel and then call gdk_pixbuf_get_from_drawable (gtk_widget_get_snapshot (label)) (a GdkPixmap is a GdkDrawable.) This will do an XGetImage to get pixels from the X server.

b.) Or you could use Cairo to draw text to the pixbuf - this is the technique that was used for the keyboard status icon in GNOME: http://blogs.gnome.org/sudaltsov/category/general/




回答2:


Example using Python and GTK3:

from gi.repository import Gtk

class TextStatusIcon:
  def __init__(self):
    self.statusicon = Gtk.StatusIcon()
    self.statusicon.connect("popup-menu", self.right_click_event)

    window = Gtk.OffscreenWindow()
    window.add(Gtk.Label("text"))
    window.connect("damage-event", self.draw_complete_event)
    window.show_all()

  def draw_complete_event(self, window, event):
    self.statusicon.set_from_pixbuf(window.get_pixbuf())

  def right_click_event(self, icon, button, time):
    Gtk.main_quit()

TextStatusIcon()
Gtk.main()

Unfortunately, most system trays limit the height of icons to a very small size, and Gtk.StatusIcon automatically scales down the PixBuf so that both the width and height are smaller than the height limit. This severely limits the amount of text you can effectively display with Gtk.StatusIcon.

See https://github.com/PaulSD/Tray_Apps/tree/master/gtktrayicon for a library (that can be installed in addition to Gtk) that provides an API for implementing more generic system tray applets, and supports arbitrary length text strings. See https://github.com/PaulSD/Tray_Apps for sample code that uses this library.



来源:https://stackoverflow.com/questions/7748183/how-can-i-display-text-on-a-statusicon-instead-of-setting-an-icon

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!