OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

PyQt: Show menu in a system tray application

  • Thread starter Thread starter Nextorlg
  • Start date Start date
N

Nextorlg

Guest
First of all, I'm an experienced C programmer but new to python. I want to create a simple application in python using pyqt. Let's imagine this application it is as simple as when it is run it has to put an icon in the system tray and it has offer an option in its menu to exit the application.

This code works, it shows the menu (I don't connect the exit action and so on to keep it simple)

Code:
import sys
from PyQt4 import QtGui

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

    trayIcon = QtGui.QSystemTrayIcon(QtGui.QIcon("Bomb.xpm"), app)
    menu = QtGui.QMenu()
    exitAction = menu.addAction("Exit")
    trayIcon.setContextMenu(menu)

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

if __name__ == '__main__':
    main()

But this doesn't:

Code:
import sys
from PyQt4 import QtGui

class SystemTrayIcon(QtGui.QSystemTrayIcon):

    def __init__(self, icon, parent=None):
        QtGui.QSystemTrayIcon.__init__(self, icon, parent)
        menu = QtGui.QMenu()
        exitAction = menu.addAction("Exit")
        self.setContextMenu(menu)

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

    trayIcon = SystemTrayIcon(QtGui.QIcon("Bomb.xpm"), app)

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

if __name__ == '__main__':
    main()

I probably miss something. There are no errors but in the second case when I click with the right button it doesn't show the menu.
<p>First of all, I'm an experienced C programmer but new to python. I want to create a simple application in python using pyqt. Let's imagine this application it is as simple as when it is run it has to put an icon in the system tray and it has offer an option in its menu to exit the application.</p>

<p>This code works, it shows the menu (I don't connect the exit action and so on to keep it simple)</p>

<pre><code>import sys
from PyQt4 import QtGui

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

trayIcon = QtGui.QSystemTrayIcon(QtGui.QIcon("Bomb.xpm"), app)
menu = QtGui.QMenu()
exitAction = menu.addAction("Exit")
trayIcon.setContextMenu(menu)

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

if __name__ == '__main__':
main()
</code></pre>

<p>But this doesn't:</p>

<pre><code>import sys
from PyQt4 import QtGui

class SystemTrayIcon(QtGui.QSystemTrayIcon):

def __init__(self, icon, parent=None):
QtGui.QSystemTrayIcon.__init__(self, icon, parent)
menu = QtGui.QMenu()
exitAction = menu.addAction("Exit")
self.setContextMenu(menu)

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

trayIcon = SystemTrayIcon(QtGui.QIcon("Bomb.xpm"), app)

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

if __name__ == '__main__':
main()
</code></pre>

<p>I probably miss something. There are no errors but in the second case when I click with the right button it doesn't show the menu.</p>
 

Latest posts

Top