QSystemTrayIcon.setIcon not working to change the tray icon

2.6k Views Asked by At
import sys
from PyQt5.QtWidgets import QSystemTrayIcon, QApplication, QMenu
from PyQt5.QtGui import QIcon

class autoparse():
    def __init__(self):

       self.main()

    def main(self):
        app = QApplication(sys.argv)

        self.trayIcon = QSystemTrayIcon(QIcon("icons\icon-windowed.ico"), app)
        self.menu = QMenu()
        self.autopconfig = self.menu.addAction('Config')
        self.autopconfig.triggered.connect(self.swapicon)
        self.trayIcon.setContextMenu(self.menu)

        self.trayIcon.show()
        sys.exit(app.exec_())
    def swapicon(self):
        QSystemTrayIcon.setIcon(QIcon("icons\python.ico"))

test1 = autoparse()

I can get the initial icon to set just fine and I can work around the issue by creating 2 icons for app and then doing a self.trayIcon.hide() and self.newicon.show() but I don't feel this is the right way to do it.

Pycharm shows a notice saying "Expected QSystemTrayIcon, got QIcon" but if I swap QIcon with QSystemTrayIcon then it says Qicon is missing. SOOOOOO I change it to QSystemTrayIcon(QIcon("icons\python.ico") and it still says QIcon is unfilled. It seems like the problem detection is going in circles.

Running the code as is gives me "setIcon (self, QIcon): first argument of unbound method must have type 'QSystemTrayIcon'" when I run the swapicon function. If I remove QIcon so its just setIcon (file) then I get "setIcon (self, QIcon): not enough arguments"

1

There are 1 best solutions below

0
On BEST ANSWER

QSystemTrayIcon is not a static element so to change some property you must use the instance, in your case you must change:

QSystemTrayIcon.setIcon(QIcon("icons\python.ico"))

to:

self.trayIcon.setIcon(QIcon("icons\python.ico"))

You may have been confused with QSystemTrayIcon(QIcon("icons\icon-windowed.ico"), app), this is a constructor and can receive the icon.