Click here to Skip to main content
15,884,099 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello again, guys. I'm stuck here -- still learning OOP and Python.

How do I structure this program so that I am able to spawn a QWidget object on start-up? Moreover, how do I make it so that each time I click on the System Tray Icon (namely mediaIcon.png), that same widget will not re-instantiate itself but, rather, show/hide itself with every other click?

As of right now, when I click on the icon, the program echos "Clicked." then closes. I don't know why that is.

Thanks a bunch,
Akratix


#!/usr/bin/python

import sys, os
from PyQt4 import QtGui, QtCore

class MediaFeed(QtGui.QSystemTrayIcon):

    def __init__(self, icon, parent=None):
        QtGui.QSystemTrayIcon.__init__(self, icon, parent)
        
        menu = QtGui.QMenu()

        aboutAction = menu.addAction("About")
        aboutAction.triggered.connect(self.aboutMenu)
    
        exitAction = menu.addAction("Exit")
        exitAction.triggered.connect(QtGui.qApp.quit)

        self.setContextMenu(menu)
        self.activated.connect(self.systemIcon)

    def aboutMenu(self):
        print "This should be some information."

    def systemIcon(self, reason):
        w = QtGui.QWidget()
        if reason == QtGui.QSystemTrayIcon.Trigger:
            print "Clicked."
            w.show()


def main():

    app = QtGui.QApplication(sys.argv)
    mediaFeed = MediaFeed(QtGui.QIcon("mediaIcon.png"), None)

    mediaFeed.show()
    sys.exit(app.exec_())


if __name__ == '__main__':
    main()
Posted
Updated 21-Oct-17 22:16pm

1 solution

modified code this worked with me. i am using Qt5. you my modify as yours

def systemIcon(self, reason):
w = QDialog()
if reason == QSystemTrayIcon.Trigger:
print ("Clicked.")
w.exec_()
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900