nos*_*one 6 python python-3.x pystray
我在寻找更改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', lambda: exit()))
global icon
icon = pystray.Icon("name", image, title, menu)
icon.run()
def show_notification(text):
icon.notify(text,"My test notification sub title")
def show():
print("show")
def show():
print("exit")
run_icon()
sleep(3)
show_notification("test")
Run Code Online (Sandbox Code Playgroud)
更新:我突然想到一个想法 - 也许这个“Python”是从项目名称或程序名称等中获取的。我应该搜索或添加与命名参数相关的代码(在 Win10 操作系统上)吗?
Python是一种解释性语言,这意味着它逐行执行代码,而不是将整个程序编译成独立的可执行文件。这意味着您的程序在编译之前并不独立存在。在 Windows 环境中,您编写的命令由python.exe.
要回答您的问题,在 Windows 中,每个通知的标题都来自属性的值File description。在你的例子中,它是“Python”,如下所示:”
鉴于此,您需要将代码转换为独立的可执行文件并填写一些属性值。这可以分两步完成:
创建一个VSVersionInfo文件(例如:version_info.rs),其中包含以下指示性内容:
VSVersionInfo(
ffi=FixedFileInfo(
OS=0x4,
fileType=0x1,
),
kids=[
StringFileInfo(
[
StringTable(
u'040904B0',
[
StringStruct(u'FileDescription', u'Tray Application'),
StringStruct(u'InternalName', u'trayapplication'),
StringStruct(u'LegalCopyright', u'Copyright (c) Andreas Violaris'),
StringStruct(u'OriginalFilename', u'trayapplication.exe'),
StringStruct(u'ProductName', u'trayapplication'),
StringStruct(u'ProductVersion', u'1.0')])
]
),
VarFileInfo([VarStruct(u'Translation', [1033, 1200])])
]
)
Run Code Online (Sandbox Code Playgroud)
VSVersionInfo结构用于存储 Windows 可执行文件的版本信息。该结构由两部分组成。“ ffi ”部分是一个FixedFileInfo结构,它存储有关文件的一般信息,例如文件类型、操作系统版本和其他属性。“ kids ”部分是存储更具体版本信息的子结构列表。
“ OS ”属性指定该文件设计的操作系统版本。该值0x4对应于Windows NT操作系统。
“ fileType ”属性指定文件的类型。该值0x1对应于一个应用程序。
StringFileInfo结构包含一系列不言自明的StringStruct结构。
VarFileInfo结构用于存储有关文件的语言和字符集的信息。它由单个VarStruct结构组成,该结构具有属性“ Translation ”和值[1033, 1200],对应于英语(美国)语言和 Unicode 字符集。
使用PyInstaller等工具将您的程序转换为独立的可执行文件。要使用PyInstaller ,您首先需要使用pip等软件包安装程序来安装它:
pip install pyinstaller
Run Code Online (Sandbox Code Playgroud)
然后,您可以使用以下PyInstaller命令将您的程序打包为可执行文件,并使用version_info.rs第一步的文件设置其版本信息:
pyinstaller --onefile main.py --version-file version_info.rs
Run Code Online (Sandbox Code Playgroud)
运行可执行文件(位于dist目录中)后,您会发现通知标题现在具有您在第一步中分配给FileDescription属性的值。
| 归档时间: |
|
| 查看次数: |
1577 次 |
| 最近记录: |