demosthenes demosthenes - 1 year ago 121
Python Question

PyQt, After MessageBox application quits, why?

i have a systray class and an Action which pop up a MessageBox.
When i click Ok to Messagebox, application quits.... why? I don't want to quit. How to fix it?

import sys
from PyQt4 import QtGui, QtCore

class SystemTrayIcon(QtGui.QSystemTrayIcon):
def __init__(self, icon, parent=None):
QtGui.QSystemTrayIcon.__init__(self, icon, parent)

menu = QtGui.QMenu(parent)
exitAction = menu.addAction("Exit")
helloAction = menu.addAction("Hello World")

QtCore.QObject.connect(exitAction, QtCore.SIGNAL('triggered()'), self.exit)
QtCore.QObject.connect(helloAction, QtCore.SIGNAL('triggered()'), self.hello)

def exit(self):

def hello(self):
msg = QtGui.QMessageBox.information(self.parent(), "Hello", "Hello World")

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

w = QtGui.QWidget()
trayIcon = SystemTrayIcon(QtGui.QIcon("qtLogo.png"), w)

if __name__ == '__main__':

Answer Source

Figured it out.

You need to set app.setQuitOnLastWindowClosed(False). So:

app = QtGui.QApplication(sys.argv)

That will stop it from exiting when the window closes.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download