Ubuntu – How to add launcher icon for python script

desktopiconslauncherpythonwindow-manager

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?

Best Answer

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

Related Question