Writing an Update notifier - some Python questions

This is what I have got so far, it runs OK, but I have a couple of queries (after the script). I ended up using pyside, it seemed to be the only one I could get to display a tray icon…

#!/usr/bin/env python

import logging
import sys, os

from PySide2.QtGui import QIcon
from PySide2 import QtWidgets
from PySide2.QtWidgets import QSystemTrayIcon, QMenu, QApplication, QAction, QMessageBox

orig_icon="icon.png"

def run_update_notifier():
    os.system("/home/xircon/scripts/updates.sh")

def show_tray_message(tray: QSystemTrayIcon):
    os.system("/home/xircon/scripts/updates.sh")
    

def which_click(reason):
    if reason == QSystemTrayIcon.ActivationReason.Trigger:
        os.system("/home/xircon/scripts/updates.sh")
        
    elif reason == QSystemTrayIcon.ActivationReason.Context:
        sys.exit() 
        print("Right-click detected")
        
    elif reason == QSystemTrayIcon.ActivationReason.MiddleClick:
        print("Middle-click detected")
        sys.exit() 
        
    else:
        print("Unknown reason")

if __name__ == '__main__':

    app = QApplication([])
    app.setQuitOnLastWindowClosed(False)

    tray = QSystemTrayIcon(QIcon(orig_icon), app)
    tray.activated.connect(which_click)
    menu = QMenu()

    action_exit = QAction("Exit")
    action_exit.triggered.connect(app.exit)
    menu.addAction(action_exit)

    tray.setContextMenu(menu)
    tray.show()

    sys.exit(app.exec_())
  • Can I change the icon to a different one on click?

  • Can I get rid of the right-click menu? Would like, click to run update script, right click to quit script. Left and middle click work fine.

For completeness, this is the update script:

#!/bin/zsh
rm /tmp/chkup.tmp
checkupdates+aur > /tmp/chkup.tmp

nlines=`wc -l /tmp/chkup.tmp | tuc -d ' ' -f 1`
echo nlines $nlines
str2="updates available"

if [[ "$nlines" == "0" ]]; then 
   str1="There are no"
fi

if [[ "$nlines" == "1" ]]; then
   str1="There is"
   str2="update available"
fi

if [[ $nlines > 1 ]]; then
   str1="There are"
fi

dat=`date +"%H:%M"`

if [[ $nlines = 0 ]]; then 
   summ="$dat - $str1 $str2:"
 else  
   summ="$dat - $str1 $nlines $str2:"
fi

notify-send -t 0 "$summ" "`cat /tmp/chkup.tmp`" 

tuc:

13 aur/tuc 0.11.0-1 (+1 0.15) (Installed)
    A more powerful alternative to cut

Makes slicing strings so much easier :smiley:

eos-notifier doesn’t show an icon in sway. Also my script gives me fine tuning.

  1. It looks like you can call PySide2.QtWidgets.QSystemTrayIcon.setIcon(icon)

  2. I am not that familiar with pyside2 but what if you omit this part?

i.e. Don’t create a context menu.

No, I am not familiar with pyside either. Will have a look after work, ended up going to bed early last night, bloody sciatica, couldn’t sit/stand (or as it turns out), lie down, TFI Friday!!!

1 Like

No, take that section out and I get a menu, with no options, just a grey rectangle.