0

I wrote a program that create a tray. I want to hide it with below code,but it not work(tray icon is visible). how solve this problem?

from PyQt5 import QtGui, QtWidgets

if __name__ == '__main__':
   app = QtWidgets.QApplication([])
   sysTray =QtWidgets.QSystemTrayIcon()
   sysTray.setIcon(QtGui.QIcon('1.jpg'))
   sysTray.hide()
   app.exec_()
eyllanesc
  • 235,170
  • 19
  • 170
  • 241
hpaalm
  • 65
  • 10

2 Answers2

1

Just add sysTray.show() before sysTray.hide()

hpaalm
  • 65
  • 10
0

Try it:

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__':
    app = QApplication(sys.argv)

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

    # Creating icon
    icon = QIcon.fromTheme("system-help", QIcon('avatar.jpg'))   # '1.jpg'

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

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

    sys.exit(app.exec_())

enter image description here

S. Nick
  • 12,879
  • 8
  • 25
  • 33
  • 1
    no, it is not answer of my question, but with your code I understand how to solve the problem. I should add `sysTray.show()` before `sysTray.hide()` . – hpaalm May 25 '19 at 21:09