为什么QSystemTrayIcon图标没有隐藏在我的系统中?

时间:2019-05-25 18:39:59

标签: python python-3.x pyqt pyqt5 qsystemtrayicon

我写了一个创建托盘的程序。我想用下面的代码将其隐藏,但它不起作用(托盘图标可见)。如何解决这个问题?

from PyQt5 import QtGui, QtWidgets

if __name__ == '__main__':
   app = QtWidgets.QApplication([])
   sysTray =QtWidgets.QSystemTrayIcon()
   sysTray.setIcon(QtGui.QIcon('1.jpg'))
   sysTray.hide()
   app.exec_()

2 个答案:

答案 0 :(得分:0)

尝试一下:

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

答案 1 :(得分:0)

只需在sysTray.show()之前添加sysTray.hide()