我尝试在 PySide6 文档中搜索有关 QMenu 和 QSystemTray 类的信息,但我无法找到有关从函数向上下文菜单添加操作的任何信息。这是我尝试过的代码:
from PySide6.QtWidgets import QSystemTrayIcon, QMenu
from PySide6 import QtWidgets
from PySide6.QtGui import QIcon, QAction
import sys
class MyWidget(QtWidgets.QSystemTrayIcon):
def __init__(self):
super().__init__()
self.trayIcon = QSystemTrayIcon()
self.trayIcon.setIcon(QIcon("icon.png"))
self.trayIcon.setVisible(True)
self.menu = QMenu()
self.quit = QAction("Quit")
self.quit.triggered.connect(app.quit)
self.menu.addAction(self.quit)
self.menu.updatesEnabled()
self.menu.setUpdatesEnabled(True)
self.trayIcon.setContextMenu(self.menu)
self.trayIcon.activated.connect(self.addMenuAction)
def addMenuAction(self):
self.menu.addAction(QAction("Action"))
self.menu.update()
if __name__ == "__main__":
app = QtWidgets.QApplication([])
widget = MyWidget()
widget.show()
sys.exit(app.exec_())