我有一个系统类和一个弹出消息框的动作。当我单击Ok to Messagebox时,应用程序退出....为什么?我不想辞职。如何解决这个问题?
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)。所以:
app = QtGui.QApplication(sys.argv)
app.setQuitOnLastWindowClosed(False)