PyQt5 QSystemTrayIcon is outside actual tray

2020-07-30 07:00发布

问题:

I have simple PyQt5 application, which is just an example, and doesn't do anything:

#!/usr/bin/env python
# -*- encoding: utf-8 -*-
import sys
from PyQt5 import QtWidgets, QtCore, QtGui 


class SystemTrayIcon(QtWidgets.QSystemTrayIcon):

    def __init__(self, icon, parent=None):
        super(SystemTrayIcon, self).__init__(icon, parent)
        menu = QtWidgets.QMenu(parent)
        exitAction = menu.addAction("Exit")
        exitAction.triggered.connect(parent.close)
        self.setContextMenu(menu)


class MainWindow(QtWidgets.QWidget):
    def __init__(self):
        super(MainWindow, self).__init__()         
        self.initUI()

    def initUI(self):      
        self.setGeometry(300, 300, 250, 150)
        self.setWindowTitle('Icon')
        self.tray_icon = SystemTrayIcon(QtGui.QIcon('test.ico'), self)
        self.tray_icon.show()
        self.show()


if __name__ == '__main__':
    app = QtWidgets.QApplication([])
    w = MainWindow()
    sys.exit(app.exec_())

After start, it shows main window and tray icon. But tray icon isn't in tray. It is in left-upper corner.

How do I fix this? This code works normally on Windows 7, Mac OS X, and Archlinux with KDE. So problem probably in Ubuntu DE.

I use latest ubuntu 14.04 amd64, python 3, PyQt5, Qt version is 5.2.1

回答1:

It's known bug; see this bug report.

Apparently, it will be fixed in next releases.