本文介绍了单击菜单项时pyqt系统托盘图标退出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我是python和pyqt的新手,今天学习了,写了一个小系统托盘恶魔,它运行,但是当我单击关于",然后关闭对话框时,应用程序退出.不知道为什么
I am new to python and pyqt, I learn them today and write a small system tray demon,it run , but when i click "about", and then close the dialog, the app exit.I don't know why
简单代码如下:
import sys
from PyQt4 import QtGui
class SystemTrayIcon(QtGui.QSystemTrayIcon):
def __init__(self, icon, parent=None):
QtGui.QSystemTrayIcon.__init__(self, icon, parent)
self.menu = QtGui.QMenu(parent)
# about action
aboutAction = QtGui.QAction("About", self)
aboutAction.triggered.connect(self.about_fun)
self.menu.addAction(aboutAction)
# quit action
quitAction = QtGui.QAction("Quit", self)
quitAction.triggered.connect(self.quit_fun)
self.menu.addAction(quitAction)
#
self.setContextMenu(self.menu)
# listen activated
self.activated.connect(self.iconActivated)
def about_fun(self):
QtGui.QMessageBox.about(self.parent(), "about", "pyqt system tray")
def quit_fun(self):
sys.exit(0)
def iconActivated(self, reason):
if reason == QtGui.QSystemTrayIcon.Trigger:
print 'left click: TODO'
def main():
app = QtGui.QApplication(sys.argv)
trayIcon = SystemTrayIcon(QtGui.QIcon("trash.svg"))
trayIcon.show()
sys.exit(app.exec_())
if __name__ == '__main__':
main()
推荐答案
您可以将应用程序的 quitOnLastWindowClosed 属性设置为 False:
You can set the application quitOnLastWindowClosed property to False:
def main():
app = QtGui.QApplication(sys.argv)
app.setQuitOnLastWindowClosed(False)
trayIcon = SystemTrayIcon(QtGui.QIcon("trash.svg"))
trayIcon.show()
sys.exit(app.exec_())
这篇关于单击菜单项时pyqt系统托盘图标退出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!