我正在努力在按下 pystray 图标时创建一个 tkinter 窗口,这样我就可以在托盘中创建自己的菜单。但我不知道如果按下托盘图标如何获取。任何帮助表示赞赏!提前致谢!
我在寻找更改pystray托盘通知标题的方法时遇到问题。看起来它从某个地方获取了默认值“Python”。见下图:
在文档中,没有额外的参数来更改通知图标标题。如何找到一种方法将标题值更改为我想要的值?
这是一个工作代码示例:
from tkinter import *
from pystray import MenuItem as item
from PIL import Image, ImageTk
from res import * #here is my base64 encoded icon. Variable icon_base64.
from base64 import b64decode
import pystray
import base64
pic=ImageTk.BytesIO(icon_base64) #transfering base64 to bytes
def run_icon():
#image = Image.open("icon.ico") #uncomment this to use a standard image, isntead of base64.
title="Tray title"
image=Image.open(pic) #comment this if using standard way of image
menu = (item('test1', lambda: show(),default = True), item('Exit', …Run Code Online (Sandbox Code Playgroud) 我希望托盘图标根据 的值进行更改p_out。具体取决于它的值,我希望它有不同的颜色。
这是代码
import pystray
import ping3
while True:
p_out = ping3.ping("google.com", unit="ms")
if p_out == 0:
img = white
elif p_out >= 999:
img = red
else:
print(f'\n{p_out:4.0f}', end='')
if p_out <= 50:
img = green
elif p_out <= 60:
img = yellow
elif p_out < 100:
img = orange
elif p_out >= 100:
img = red
icon = pystray.Icon(" ", img)
icon.run()
Run Code Online (Sandbox Code Playgroud)
我尝试在每个循环上“重置”pystray,icon但它不起作用。仅当我停止并重新运行脚本时,图标才会发生变化。
我正在尝试创建一个 pystray 菜单分隔符,但我很难做到这一点。我在这里搜索了 SO 及其文档,我发现这非常令人困惑且无益,我什至尝试阅读该Menu课程:
class Menu(object):
"""A description of a menu.
A menu description is immutable.
It is created with a sequence of :class:`Menu.Item` instances, or a single
callable which must return a generator for the menu items.
First, non-visible menu items are removed from the list, then any instances
of :attr:`SEPARATOR` occurring at the head or tail of the item list are
removed, and any consecutive separators are reduced to one.
"""
#: A …Run Code Online (Sandbox Code Playgroud) 我想改变一个项目的文本。为此,我尝试使用 update_menu() 更新菜单。不幸的是,这不起作用,我在 pystray 文档中找不到更详细的内容。我希望你可以帮助我。谢谢。
from pystray import Icon as icon, Menu as menu, MenuItem as item
import PIL.Image
image = PIL.Image.new('RGB', (100, 100), 255)
adapter = 'before'
def test():
global ps
global adapter
adapter = 'after'
ps.update_menu()
ps = icon(name='name', icon=image, menu=menu(
item(text='Adapter', action=menu(item(text='Test', action=test))),
item(text=adapter, action=lambda: test()),
)
)
ps.run()
Run Code Online (Sandbox Code Playgroud) pystray ×5
python ×5
python-3.x ×2
menu ×1
separator ×1
system-tray ×1
taskbar ×1
tkinter ×1
windows ×1