Pystray doesn't work inside snap

pystray icon doesn’t work inside snap.

Here my code, you can clone, do snapcraft, snap install tray_1_....snap --dangerous --devmode , and snap run tray , but it doesn’t work, doesn’t show the tray icon.

If you try to do ./tray.py it will work.

Am I missing something?

Some interface? connection? plug?

My OS host is Ubuntu 20.04

tray.py:

#!/usr/bin/env python3
# coding=utf-8

from tkinter import *
from pystray import Icon as icon, Menu as menu, MenuItem as item
import pystray
from PIL import Image
import sys

window = Tk()

def quit_window():
  sys.exit()

def hide_window():
    window.withdraw()
    items = []
    items.append(pystray.MenuItem('Quit', quit_window))
    image=Image.open('./app.png')
    icon=pystray.Icon("name", image, "title", items)
    icon.run()

window.protocol('WM_DELETE_WINDOW', hide_window)
window.mainloop()

snapcraft:

name: tray # you probably want to 'snapcraft register <name>'
base: core20 # the base snap is the execution environment for this snap
version: '1' # just for humans, typically '1.2+git' or '1.3.2'
summary: Single-line elevator pitch for your amazing snap # 79 char long summary
description: |
  This is my-snap's description. You have a paragraph or two to tell the
  most important story about your snap. Keep it under 100 words though,
  we live in tweetspace and your description wants to look good in the snap
  store.

grade: devel # must be 'stable' to release into candidate/stable channels
confinement: devmode # use 'strict' once you have the right plugs and slots

apps:
  tray:
    command: bin/tray.py
    plugs:
    - home
    - x11
    - desktop
    - wayland
    command-chain:
    - bin/debian-multiarch-triplet-provider-launch
    - bin/tcltk-launch
    environment:
      PYTHONPATH: $SNAP/usr/lib/python3.8:$SNAP/usr/lib/python3.8/lib-dynload:$SNAP/usr/lib/python3/dist-packages:$SNAP/lib/python3.8/site-packages:$SNAP/usr/lib/x86_64-linux-gnu/pulseaudio:$PYTHONPATH

parts:
  tray:
    plugin: python
    source: .
    stage-packages:
      - python3.8-tk
    python-packages:
      - pystray

  dump-files:
    plugin: dump
    source: .
    organize:
      tray.py: bin/
      app.ico: bin/

  debian-multiarch-triplet-provider-launch:
    source: https://github.com/Lin-Buo-Ren/debian-multiarch-triplet-provider-launch.git
    source-tag: v1.0.0
    plugin: dump
    stage:
    - bin/debian-multiarch-triplet-provider-launch

  launcher:
    source: https://github.com/Lin-Buo-Ren/tcltk-launch.git
    source-tag: v1.0.0
    plugin: dump
    stage:
    - bin/*

layout:
  /usr/share/tcltk:
    bind: $SNAP/usr/share/tcltk
  /usr/lib/tcltk:
    bind: $SNAP/usr/lib/tcltk

Pystray needs to use a backend, it can be appindicator, gtk, xorg.
To use gtk I needed to install it.

debian libraries:
python3-gi python3-gi-cairo gir1.2-gtk-3.0 libcairo2-dev pkg-config libgirepository1.0-dev libglib2.0-dev libffi-dev

python libraries:
Pillow

and configure path variables:
PYSTRAY_BACKEND=gtk
PYTHONPATH=$SNAP/usr/lib/python3.8/lib-dynload:$SNAP/usr/lib/python3.8/tkinter:$SNAP/usr/lib/python3.8/share/lintian/overrides:$PYTHONPATH

1 Like