Python PyQt,在MessageBox应用程序退出后,为什么?

Python PyQt,在MessageBox应用程序退出后,为什么?,python,pyqt,exit,messagebox,Python,Pyqt,Exit,Messagebox,我有一个systray类和一个弹出消息框的操作。 当我单击确定消息框时,应用程序退出。。。。为什么?我不想放弃。如何修复它 import sys from PyQt4 import QtGui, QtCore class SystemTrayIcon(QtGui.QSystemTrayIcon): def __init__(self, icon, parent=None): QtGui.QSystemTrayIcon.__init__(self, icon, paren

我有一个systray类和一个弹出消息框的操作。 当我单击确定消息框时,应用程序退出。。。。为什么?我不想放弃。如何修复它

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")

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

    def exit(self):
        QtCore.QCoreApplication.exit()

    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)

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

if __name__ == '__main__':
    main()
我明白了

您需要设置app.setQuitOnLastWindowClosed(False)。因此:


这将阻止它在窗口关闭时退出。

主窗口小部件被隐藏。如果我调用w.show(),它会显示一个小部件,应用程序不会退出。但我不想显示小部件。这只是一个systray应用程序。是的,它很有趣。我已经把你的代码复制了你的问题。如果我弄明白了,我会更新。是的,app.setQuitOnLastWindowClosed(False)完成了任务!谢谢
app = QtGui.QApplication(sys.argv)
app.setQuitOnLastWindowClosed(False)