Reputation: 312
I have an issue with finding a way to change the pystray tray notification title. It appears that it's taking a default value of "Python" from somewhere. See the image below:
In the documentation, there are no additional parameters to change the notification icon title. How can I find a way to change the title value to something that I want?
Here is a working code example:
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")
Update: An idea just came to my head - perhaps this "Python" is being taken from the project name or program name, etc. Should I search or add code related to naming parameters (on a Win10 OS)?
Upvotes: 6
Views: 2102
Reputation: 4198
Python is an interpreted language, which means that it executes code line by line rather than compiling the entire program into a standalone executable. This means that your program does not have a standalone existence until you compile it. In a Windows environment, the commands you have written are executed by python.exe
.
To answer your question, in Windows, the title of each notification comes from the value of the File description
property. In your case, it is "Python" as shown below:"
Given this, you need to turn your code into a standalone executable file and fill in some property values. This can be done in two steps:
Create a VSVersionInfo file (e.g.: version_info.rs
), with the following indicative content:
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])])
]
)
The VSVersionInfo structure is used to store version information for a Windows executable file. The structure consists of two parts. The "ffi" part is a FixedFileInfo structure, which stores general information about the file, such as the file type, operating system version, and other attributes. The "kids" part is a list of sub-structures that store more specific version information.
The "OS" property specifies the operating system version for which the file was designed. The value 0x4
corresponds to the Windows NT operating system.
The "fileType" property specifies the type of file. The value 0x1
corresponds to an application.
The StringFileInfo structure contains a list of StringStruct structures that are self-explanatory.
The VarFileInfo structure is used to store information about the language and character set of the file. It consists of a single VarStruct structure with the property "Translation" and the value [1033, 1200]
, which corresponds to the English (US) language and the Unicode character set.
Turn your program into a standalone executable using a tool like PyInstaller. To use PyInstaller, you first need to install it using a package installer like pip:
pip install pyinstaller
Then, you can use the following PyInstaller command to package your program into an executable and set its version information using the version_info.rs
file of the first step:
pyinstaller --onefile main.py --version-file version_info.rs
After running the executable (located in the dist directory), you will find that the notification title now has the value you assigned to the FileDescription property in the first step.
Upvotes: 7