from PyQt5 import QtWidgets from PyQt5.QtGui import QPixmap, QIcon import caller_src_rc class TrayIcon(QtWidgets.QSystemTrayIcon): def __init__(self, MainWindow, parent=None): super(TrayIcon, self).__init__(parent) self.ui = MainWindow # 需要设置图标才能正常显示 self.setIcon(QIcon(":/caller_icon.png")) self.creatMenu() # 触发单击右键显示菜单 self.activated.connect(self.trayIconClicked) def creatMenu(self): # 托盘图标创建右键菜单 self.menu = QtWidgets.QMenu() self.menu.addAction(QtWidgets.QAction( "居中显示", self, triggered=self.showInMidScreen)) self.menu.addAction(QtWidgets.QAction( "退出", self, triggered=self.exitApp)) self.setContextMenu(self.menu) def showInMidScreen(): print('居中显示') def exitApp(): print('退出') def trayIconClicked(self, reason): if reason == 1: self.menu.exec_()