Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>This drove me nuts for days. Complete app code to implement below.</p> <p>Key bits:</p> <ul> <li>override closeEvent(), enabling it to do either of just hiding window or true exit</li> <li>create some facility for user to choose either hide or exit behavior</li> <li>don't show() main window on instantiation, just exec_() the App</li> </ul> <blockquote> <pre><code>import sys from PyQt4.QtGui import QAction, QApplication, QFrame, QIcon, \ QMainWindow, QMenu, QSystemTrayIcon from PyQt4.QtCore import SIGNAL class MyApp(QMainWindow): def __init__(self, parent, title): super(QMainWindow, self).__init__(parent) self.exitOnClose = False exit = QAction(QIcon(), "Exit", self) self.connect(exit, SIGNAL("triggered()"), self.exitEvent) self.trayIcon = QSystemTrayIcon(QIcon(), self) menu = QMenu(self) menu.addAction(exit) self.trayIcon.setContextMenu(menu) self.connect(self.trayIcon, \ SIGNAL("activated(QSystemTrayIcon::ActivationReason)"), \ self.trayIconActivated) self.trayIcon.show() self.trayIcon.showMessage("MyApp is running!", "Click to open window\nRight click for menu" ) def trayIconActivated(self, reason): if reason == QSystemTrayIcon.Context: self.trayIcon.contextMenu().show() elif reason == QSystemTrayIcon.Trigger: self.show() self.raise_() def closeEvent(self, event): if self.exitOnClose: self.trayIcon.hide() del self.trayIcon event.accept() else: self.hide() event.setAccepted(True) event.ignore() def exitEvent(self): self.exitOnClose = True self.close() if __name__ == "__main__": app = QApplication(sys.argv) myapp = MyApp(None, "My System Tray App") app.exec_() </code></pre> </blockquote>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload