托盘图标的文本叠加

发布于 2024-10-12 00:51:03 字数 439 浏览 1 评论 0原文

我有一个使用 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 创建单独的图像?

I have a simple tray icon using PyGTK's 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()

How can I add a text label (one or two characters; basically, unread count) to the tooltip - without creating separate images for set_from_file?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

Bonjour°[大白 2024-10-19 00:51:03

在 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

A somewhat simpler example using Gtk.OffscreenWindow in GTK3:

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

(See also stackoverflow.com/a/26208202/1476175)

心凉 2024-10-19 00:51:03
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)
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)
酒绊 2024-10-19 00:51:03

Pango 想要 Widget 类来绘图,但 StatusIcon 不需要。

import gtk
from gtk import gdk
import cairo

traySize = 24

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

pixmap = trayPixbuf.render_pixmap_and_mask(alpha_threshold=127)[0] ## pixmap is also a drawable
cr = pixmap.cairo_create() # https://developer.gnome.org/gdk/unstable/gdk-Cairo-Interaction.html#gdk-cairo-create

# drawing
cr.select_font_face("Georgia", cairo.FONT_SLANT_NORMAL, cairo.FONT_WEIGHT_BOLD)
cr.set_source_rgba(0, 0, 0, 0)
cr.set_source_rgba(1, 0.1, 0.5, 1)
cr.set_font_size(30)
cr.move_to(0, 16)
cr.show_text("a")

# surf.write_to_png('test.png')

trayPixbuf.get_from_drawable(pixmap, pixmap.get_colormap(), 0, 0, 0, 0, traySize, traySize) # not sure 2nd arg is ok
trayPixbuf = trayPixbuf.add_alpha(True, 0x00, 0x00, 0x00)
statusIcon.set_from_pixbuf(trayPixbuf)

statusIcon.set_visible(True)

gtk.main()

Pango wants Widget class for drawing but StatusIcon is not.

import gtk
from gtk import gdk
import cairo

traySize = 24

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

pixmap = trayPixbuf.render_pixmap_and_mask(alpha_threshold=127)[0] ## pixmap is also a drawable
cr = pixmap.cairo_create() # https://developer.gnome.org/gdk/unstable/gdk-Cairo-Interaction.html#gdk-cairo-create

# drawing
cr.select_font_face("Georgia", cairo.FONT_SLANT_NORMAL, cairo.FONT_WEIGHT_BOLD)
cr.set_source_rgba(0, 0, 0, 0)
cr.set_source_rgba(1, 0.1, 0.5, 1)
cr.set_font_size(30)
cr.move_to(0, 16)
cr.show_text("a")

# surf.write_to_png('test.png')

trayPixbuf.get_from_drawable(pixmap, pixmap.get_colormap(), 0, 0, 0, 0, traySize, traySize) # not sure 2nd arg is ok
trayPixbuf = trayPixbuf.add_alpha(True, 0x00, 0x00, 0x00)
statusIcon.set_from_pixbuf(trayPixbuf)

statusIcon.set_visible(True)

gtk.main()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文