Skip to main content
 首页 » 编程设计

python之托盘图标的文本覆盖

2025年05月04日7shangdawei

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

import pygtk 
pygtk.require('2.0') 
import gtk 
 
 
statusIcon = gtk.StatusIcon() 
statusIcon.set_from_stock(gtk.STOCK_EDIT) 
statusIcon.set_tooltip('Hello World') 
statusIcon.set_visible(True) 
 
gtk.main() 

如何在不为 set_from_file 创建单独的图像的情况下向工具提示添加文本标签(一个或两个字符;基本上是未读计数)?

请您参考如下方法:

在 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() 

(另见 stackoverflow.com/a/26208202/1476175)