I've knocked together a simple PyQT5 app that displays a trayicon.
On my mac it works:
However on my client's mac it doesn't display. (Alas, I do not have a screenshot).
I noticed my client has more items on his tray, so my first thought was that there's a limit, and he had hit that limit.
However, removing one icon and re-running, still it does not show up on his system. Which would appear to debunk this hypothesis.
Can anyone think why it's not working?
Here's the code:
import logging
import signal
import sys
import subprocess
import os
from PyQt5 import QtGui, QtWidgets, QtCore
logger = logging.getLogger(__name__)
# This is't used...
def refresh(*args):
logger.warning('Hello')
class Application(QtWidgets.QApplication):
def event(self, e):
return QtWidgets.QApplication.event(self, e)
class Application(QtWidgets.QApplication):
def launch_terminal(self):
# start backup script
cmd = f"cd {os.getcwd()}; env -i PATH=$PATH bash -c ./backup.sh"
subprocess.run(['osascript', '-e', f'tell application "Terminal" to do script "{cmd}"'])
def check_backup():
if os.path.exists('/tmp/backup_done'):
os.remove('/tmp/backup_done')
self.quit()
# start timer to check if backup is completed
self.timer = QtCore.QTimer()
self.timer.timeout.connect(check_backup)
self.timer.start(1000) # check every second
class SystemTrayIcon(QtWidgets.QSystemTrayIcon):
def __init__(self, parent=None):
super().__init__(parent)
self.menu = QtWidgets.QMenu(parent)
self.refresh_action = self.menu.addAction("Refresh")
self.refresh_action.triggered.connect(refresh)
self.exit_action = self.menu.addAction("Exit")
self.exit_action.triggered.connect(QtWidgets.qApp.quit)
self.setContextMenu(self.menu)
# Create a pixmap from a Unicode character
font = QtGui.QFont("Arial", 30)
pixmap = QtGui.QPixmap(50, 50)
pixmap.fill(QtCore.Qt.transparent)
painter = QtGui.QPainter(pixmap)
painter.setFont(font)
painter.drawText(pixmap.rect(), QtCore.Qt.AlignCenter, "")
painter.end()
self.setIcon(QtGui.QIcon(pixmap))
def main():
app = Application(sys.argv)
# Registers a signal handler to exit the application gracefully
# when receiving a SIGINT signal (e.g., Ctrl+C on the terminal).
signal.signal(signal.SIGINT, lambda *a: app.quit())
tray_icon = SystemTrayIcon()
tray_icon.exit_action.triggered.connect(app.quit)
tray_icon.show()
# Timer calls Application.event repeatedly.
app.startTimer(200)
app.launch_terminal()
sys.exit(app.exec_())
if __name__ == '__main__':
main()