QSystemTrayIcon 不显示 [英] QSystemTrayIcon not showing

查看:37
本文介绍了QSystemTrayIcon 不显示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用 Kubuntu 18.04 (qt5 5.9.5),Python 3.6.我无法使用此代码来显示托盘图标;其他图标(如 Dropbox 等)也会显示,但不会显示:

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_())

显示了消息(hoge"、moge"),但我在任何地方都找不到图标......左上角都没有,如 其他帖子说.

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

推荐答案

不知道为什么,但是这段代码有效,sing PySide2(基本上和上面的代码是一样的...):

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_())

这篇关于QSystemTrayIcon 不显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆