How can I display text on a StatusIcon instead of

2019-05-07 08:02发布

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.

标签: python gtk pygtk
2条回答
Emotional °昔
2楼-- · 2019-05-07 08:37

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.

查看更多
做自己的国王
3楼-- · 2019-05-07 08:48

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/

查看更多
登录 后发表回答