Python 托盘图标的文本覆盖

Python 托盘图标的文本覆盖,python,gtk,pygtk,Python,Gtk,Pygtk,我有一个使用PyGTK的简单托盘图标: 如何在工具提示中添加文本标签(一个或两个字符;基本上是未读计数),而不从文件中为set\u创建单独的图像?Pango想要用于绘图的小部件类,但StatusIcon没有 import gtk from gtk import gdk traySize = 24 statusIcon = gtk.StatusIcon() trayPixbuf = gdk.Pixbuf(gdk.COLORSPACE_RGB, True, 8, traySize, traySi

我有一个使用PyGTK的简单托盘图标:


如何在工具提示中添加文本标签(一个或两个字符;基本上是未读计数),而不从文件中为
set\u创建单独的图像?

Pango想要用于绘图的小部件类,但StatusIcon没有

import gtk
from gtk import gdk

traySize = 24

statusIcon = gtk.StatusIcon()
trayPixbuf = gdk.Pixbuf(gdk.COLORSPACE_RGB, True, 8, traySize, traySize)

## Every time you want to change the image/text of status icon:
pixbuf = gtk.image_new_from_stock(gtk.STOCK_EDIT, traySize).get_pixbuf()
pixmap = pixbuf.render_pixmap_and_mask(alpha_threshold=127)[0] ## pixmap is also a drawable
textLay = statusIcon.create_pango_layout('Hi')

## Calculate text position (or set it manually)
(text_w, text_h) = textLay.get_pixel_size()
x = (traySize-text_w) / 2
y = traySize/4 + int((0.9*traySize - text_h)/2)

## Finally draw the text and apply in status icon
pixmap.draw_layout(pixmap.new_gc(), x, y, textLay, gdk.Color(255, 0, 0))## , foreground, background)
trayPixbuf.get_from_drawable(pixmap, self.get_screen().get_system_colormap(), 0, 0, 0, 0, traySize, traySize)
statusIcon.set_from_pixbuf(trayPixbuf)
导入gtk
从gtk导入gdk
进口开罗
traySize=24
statusIcon=gtk.statusIcon()
trayPixbuf=gdk.Pixbuf(gdk.COLORSPACE_RGB,True,8,traySize,traySize)
pixmap=trayPixbuf.render_pixmap_和_mask(alpha_threshold=127)[0]35;#pixmap也是可绘制的
cr=pixmap.cairo_create()https://developer.gnome.org/gdk/unstable/gdk-Cairo-Interaction.html#gdk-开罗创造
#绘图
cr.select\u font\u face(“格鲁吉亚”,cairo.font\u斜面\u法线,cairo.font\u重量\u粗体)
cr.set_source_rgba(0,0,0,0)
cr.set\u source\u rgba(1,0.1,0.5,1)
cr.set字体大小(30)
cr.move_至(0,16)
cr.显示文字(“a”)
#surf.write_to_png('test.png'))
trayPixbuf.get_from_drawable(pixmap,pixmap.get_colormap(),0,0,0,traySize,traySize)#不确定第二个参数是否正常
trayPixbuf=trayPixbuf.add_alpha(True,0x00,0x00,0x00)
statusIcon.set_from_pixbuf(trayPixbuf)
状态图标。设置为可见(真)
gtk.main()

在GTK3中使用Gtk.OffscreenWindow的一个稍微简单的示例:

from gi.repository import Gtk

statusIcon = Gtk.StatusIcon()
window = Gtk.OffscreenWindow()
window.add(Gtk.Label("text"))
def draw_complete_event(window, event, statusIcon=statusIcon):
  statusIcon.set_from_pixbuf(window.get_pixbuf())
window.connect("damage-event", draw_complete_event)
window.show_all()

Gtk.main()

(另请参见)

我认为您必须删除此问题,只需坚持您在此处发布的新问题:@singularity:为什么?它们是完全不同的问题。谢谢,这看起来很有希望——但我无法让它发挥作用:我使用
import gtk.gdk as gdk
gnome.ui as ui
来填补空白,但两者似乎都不对(例如,“AttributeError:'module'对象没有“pixbuf_new_from_stock”属性)“AttributeError:'模块'对象没有属性'traySize'”)。谢谢!不幸的是,我现在得到的是“ValueError:图像应该是GdkPixbuf或空的”(l.10,
get\u pixbuf
)。我找到了-但是
statusIcon
不是
GtkWidget
,所以我尝试了
gtk.Widget.render\u图标(img,”,traySize)
-这也不起作用。。。
from gi.repository import Gtk

statusIcon = Gtk.StatusIcon()
window = Gtk.OffscreenWindow()
window.add(Gtk.Label("text"))
def draw_complete_event(window, event, statusIcon=statusIcon):
  statusIcon.set_from_pixbuf(window.get_pixbuf())
window.connect("damage-event", draw_complete_event)
window.show_all()

Gtk.main()