gpt4 book ai didi

python - 单击菜单项时pyqt系统托盘图标退出

转载 作者:行者123 更新时间:2023-11-30 22:54:10 26 4
gpt4 key购买 nike

我是python和pyqt的新手,今天学习它们并编写了一个小的系统托盘恶魔,它运行,但是当我单击“关于”,然后关闭对话框时,应用程序退出。我不知道为什么

简单代码如下:

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:

def main():
app = QtGui.QApplication(sys.argv)
app.setQuitOnLastWindowClosed(False)
trayIcon = SystemTrayIcon(QtGui.QIcon("trash.svg"))
trayIcon.show()
sys.exit(app.exec_())

关于python - 单击菜单项时pyqt系统托盘图标退出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37919169/

26 4 0