import gi, platform gi.require_version("Gtk", "3.0") from gi.repository import Gtk # 需要安装这个包 gir1.2-gdkpixbuf-2.0 from gi.repository.GdkPixbuf import Pixbuf # 优先尝试使用指示器, 没有再使用 Gtk.StatusIcon try: gi.require_version('AyatanaAppIndicator3', '0.1') # 需要安装这个包 gir1.2-ayatanaappindicator3-0.1 from gi.repository import AyatanaAppIndicator3 as AppIndicator3 except (ValueError, ImportError): AppIndicator3 = None # 是否 widnows def is_windows(): return platform.system() in ('Windows', 'Microsoft') # 文件要绝对路径 def get_pixbuf(filename: str, size: int = 0) -> Pixbuf: if size: pixbuf = Pixbuf.new_from_file_at_size(filename, size, size) else: pixbuf = Pixbuf.new_from_file(filename) return pixbuf # 获取logo图标 def get_logo(size): filename = 'youtube.svg' # windows要用png格式 if is_windows(): filename = 'youtube.png' return get_pixbuf(filename, size) def create_tray(): if AppIndicator3 : indicator = AppIndicator3.Indicator.new( "youtube", "youtube", AppIndicator3.IndicatorCategory.APPLICATION_STATUS ) else: # windows 和 macos 必须传二进制图标, linux可传图标名称(自会去主题中找) indicator = Gtk.StatusIcon.new_from_pixbuf(get_logo(32)) # linux indicator = Gtk.StatusIcon.new_from_icon_name('youtube') return indicator