0

Using Kubuntu 18.04 (qt5 5.9.5), Python 3.6. I can't get this code to show the tray icon; other icons like Dropbox, etc. are shown, but this not:

import sys

from PyQt5.QtWidgets import QApplication, QMenu, QSystemTrayIcon, qApp, QMessageBox
from PyQt5.QtGui import QIcon


def run_something():
    print("Running something...")


if __name__ == '__main__':

    print("Creating application...")
    app = QApplication(sys.argv)

    print("Creating menu...")
    menu = QMenu()
    checkAction = menu.addAction("Check Now")
    checkAction.triggered.connect(run_something)
    quitAction = menu.addAction("Quit")
    quitAction.triggered.connect(qApp.quit)

    print("Creating icon...")
    icon = QIcon.fromTheme("system-help")

    print("Creating tray...")
    trayIcon = QSystemTrayIcon(icon, app)
    trayIcon.setContextMenu(menu)

    print("Showing tray...")
    trayIcon.show()
    trayIcon.setToolTip("unko!")
    trayIcon.showMessage("hoge", "moge")

    print("Running application...")
    sys.exit(app.exec_())

The message ("hoge", "moge") is shown, but I can't find the icon anywhere... Neither in the left upper corner, as other post says.

eyllanesc
  • 235,170
  • 19
  • 170
  • 241
okelet
  • 716
  • 1
  • 8
  • 23
  • `QIcon QIcon::fromTheme(const QString &name, const QIcon &fallback)` Returns the QIcon corresponding to name in the current icon theme. If no such icon is found in the current theme fallback is returned instead. If you want to provide a guaranteed fallback for platforms that do not support theme icons, `you can use the second argument` : `icon = QIcon.fromTheme("system-help", QIcon('branch-end.png'))` – S. Nick Nov 22 '18 at 23:44
  • Thanks @S.Nick, also tried with a local icon without success. – okelet Nov 23 '18 at 07:14
  • Sorry, everything works in Windows 10. – S. Nick Nov 23 '18 at 07:36
  • Also tried with a clean installation of Kubuntu 18.10, same results. – okelet Nov 23 '18 at 10:45

2 Answers2

4

No idea why, but this code works, sing PySide2 (basically is the same code than above...):

import logging
import sys

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


def run_something():
    print("Running something...")


def show_message():
    msg = QMessageBox()
    msg.setIcon(QMessageBox.Information)

    msg.setWindowTitle("MessageBox demo")
    msg.setText("This is a message box")

    msg.setInformativeText("This is additional information")
    msg.setDetailedText("The details are as follows:")
    msg.setStandardButtons(QMessageBox.Ok | QMessageBox.Cancel)
    msg.exec_()


def show_tray_message(tray: QSystemTrayIcon):
    tray.showMessage("Hoooo", "Message from tray")


if __name__ == '__main__':

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

    tray = QSystemTrayIcon(QIcon("acorn.png"), app)
    menu = QMenu()

    action_test = QAction("Show a message box")
    action_test.triggered.connect(show_message)
    menu.addAction(action_test)

    action_tray_message = QAction("Show a message from tray")
    action_tray_message.triggered.connect(lambda: show_tray_message(tray))
    menu.addAction(action_tray_message)

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

    tray.setContextMenu(menu)
    tray.setToolTip("Tool tip")
    tray.show()

    sys.exit(app.exec_())
okelet
  • 716
  • 1
  • 8
  • 23
1

Try PySide2. I just got PySimpleGUIQt running with QSystemTrayIcon today on Linux and Windows with PySide2. Maybe you'll have better luck there?

Mike from PSG
  • 5,312
  • 21
  • 39