How to add launcher icon for python script?

9,384

Due to the fact that your question is nicely documented, we can find the problem :)

  • In your application, you set:

    root = tk.Tk(className='MyTkApp')
    
  • Now if you open a terminal, type:

    $ xprop WM_CLASS
    

    and subsequently click on the window of your application, it shows:

    WM_CLASS(STRING) = "myTkApp", "Mytkapp"
    

This is just not the same as the window class you set; the capitals are different. The only conclusion is that for some reason, the window class (capital-format) you set is not accepted by the window manager.

When I changed your launcher to match myTkApp:

StartupWMClass=myTkApp

it works fine (for convenience reason, I set the firefox icon, since I don't have your icon):

[Desktop Entry]
Type=Application
Terminal=false
Name=My Tk Application
Exec=/home/jacob/Bureaublad/testapp.py
Icon=firefox
StartupWMClass=myTkApp

The result:

enter image description here

Share:
9,384

Related videos on Youtube

Håkon Hægland
Author by

Håkon Hægland

Updated on September 18, 2022

Comments

  • Håkon Hægland
    Håkon Hægland over 1 year

    I am trying to add a launcher icon for a custom Python script showing a Tkinter window (script location: /home/hakon/my-tkapp.py):

    #! /usr/bin/env python3
    import tkinter as tk
    root = tk.Tk(className='MyTkApp')
    label = tk.Label(root, text="Hello World")
    label.pack()
    root.mainloop()
    

    The script is executable. I am using pyenv, so if I run the following from gnome-terminal:

    $ which python3
    /home/hakon/.pyenv/shims/python3
    

    I created a desktop file (file location: ~/.local/share/applications/my-tk-app.desktop):

    [Desktop Entry]
    Type=Application
    Terminal=false
    Name=My Tk Application
    Exec=/home/hakon/my-tkapp.py
    Icon=/home/hakon/icons/my-tk-app-icon.png
    StartupWMClass=MyTkApp
    

    For the icon, I just (for the purpose of testing) copied one of the standard icons:

    cp /usr/share/icons/hicolor/48/apps/apport.png /home/hakon/icons/my-tk-app-icon.png
    

    Running the desktop-file-validate command on the desktop file gives no output, so the desktop file should be OK.

    However, when I run the python script from the terminal:

    ~/my-tkapp.py
    

    I still get the generic question mark icon in the launcher.

    What am I overlooking here?

  • Håkon Hægland
    Håkon Hægland almost 7 years
    Hi Jacob, now I got another problem that I think is related to tkinter, see this question
  • Pedroski
    Pedroski almost 4 years
    I wanted to do this too! I made an icon and followed your and Hakon's instructions.Worked first time! Fantastic! Thank you both!
  • saulspatz
    saulspatz about 2 years
    Didn't work for me.