pythonpython-3.xpystray

Change pystray tray notification title


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:

image

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)?


Solution

  • 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:"

    python.exe properties

    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:

    STEP 1

    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])])
        ]
    )
    

    TL;DR:

    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 "ffi" part of the VSVersionInfo structure contains a FixedFileInfo structure.

    The "kids" part of the VSVersionInfo structure contains a list with two elements: a StringFileInfo structure and a VarFileInfo structure.

    STEP 2

    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
    

    RESULT

    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.

    result