This repository has been archived on 2023-09-06. You can view files and clone it, but cannot push or open issues/pull-requests.
yutent
/
py-gtk-notes
Archived
1
0
Fork 0

tray点击

master
yutent 2023-07-24 19:37:20 +08:00
parent e5642a10ac
commit 2b0afc5d1b
2 changed files with 68 additions and 15 deletions

28
main.py
View File

@ -7,13 +7,6 @@ gi.require_version("WebKit2", "4.1")
from gi.repository import Gtk, WebKit2
# 优先尝试使用指示器, 没有再使用 Gtk.StatusIcon
try:
gi.require_version('AyatanaAppIndicator3', '0.1')
from gi.repository import AyatanaAppIndicator3 as AppIndicator3
except (ValueError, ImportError):
AppIndicator3 = None
class WebKitWindow(Gtk.Window):
def __init__(self):
@ -60,16 +53,21 @@ class WebKitWindow(Gtk.Window):
def create_tray(self):
if AppIndicator3 :
indicator = AppIndicator3.Indicator.new(
"youtube",
"youtube",
AppIndicator3.IndicatorCategory.APPLICATION_STATUS
)
else:
indicator = Gtk.StatusIcon.new_from_icon_name('youtube')
indicator = Gtk.StatusIcon.new_from_icon_name('youtube')
indicator.connect('activate', self.toggle_visible)
return indicator
def toggle_visible(self, icon):
# trayIcon
print(icon)
if self.is_active():
self.hide()
else:
self.present()
def file_path(self, filepath):

55
notes/tray.py Normal file
View File

@ -0,0 +1,55 @@
import gi, platform
gi.require_version("Gtk", "3.0")
from gi.repository import Gtk
from gi.repository.GdkPixbuf import Pixbuf
# 优先尝试使用指示器, 没有再使用 Gtk.StatusIcon
try:
gi.require_version('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