本文整理汇总了Python中PyQt4.QtGui.QSystemTrayIcon.contextMenu方法的典型用法代码示例。如果您正苦于以下问题:Python QSystemTrayIcon.contextMenu方法的具体用法?Python QSystemTrayIcon.contextMenu怎么用?Python QSystemTrayIcon.contextMenu使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类PyQt4.QtGui.QSystemTrayIcon
的用法示例。
在下文中一共展示了QSystemTrayIcon.contextMenu方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: LunchinatorGuiController
# 需要导入模块: from PyQt4.QtGui import QSystemTrayIcon [as 别名]
# 或者: from PyQt4.QtGui.QSystemTrayIcon import contextMenu [as 别名]
#.........这里部分代码省略.........
if hasattr(QIcon, "fromTheme"):
icon = QIcon.fromTheme(name, QIcon(icon_file))
if not icon:
icon = QIcon(icon_file)
self.statusicon.setIcon(icon)
def createTrayIcon(self):
if platform.linux_distribution()[0] == "Ubuntu":
if not os.path.exists('/usr/share/icons/ubuntu-mono-light/status/24/lunchinator.svg') or \
not os.path.exists('/usr/share/icons/ubuntu-mono-dark/status/24/lunchinator.svg'):
result = QMessageBox.question(self.mainWindow,
"Install Icons",
"Do you want to install the Lunchinator icons into the Ubuntu theme folders? You will have to enter your sudo password.",
buttons=QMessageBox.Yes | QMessageBox.No,
defaultButton=QMessageBox.Yes)
if result == QMessageBox.Yes:
if subprocess.call(['gksu', get_settings().get_resource('bin', 'install-lunch-icons.sh') + ' lunchinator']) == 0:
getCoreLogger().info("restarting after icons were installed")
restart(getCoreLogger())
return False
else:
QMessageBox.critical(self.mainWindow,
"Error installing icons",
"The icons were not installed, there was an error.",
buttons=QMessageBox.Ok,
defaultButton=QMessageBox.Ok)
getCoreLogger().info("icons were not installed because of an error")
# initialize tray icon
self.statusicon = QSystemTrayIcon(self.mainWindow)
# _highlightIcon sets the default icon
self._highlightIcon()
contextMenu = self.init_menu(self.mainWindow)
self.statusicon.activated.connect(self.trayActivated)
self.statusicon.setContextMenu(contextMenu)
self.statusicon.show()
return True
@loggingSlot(QSystemTrayIcon.ActivationReason)
def trayActivated(self, reason):
if getPlatform() == PLATFORM_MAC:
# Trigger is sent even though the context menu is shown.
return
if reason == QSystemTrayIcon.Trigger:
self.statusicon.contextMenu().popup(QCursor.pos())
def _coldShutdown(self, exitCode=0):
# before exiting, process remaining events (e.g., pending messages like HELO_LEAVE)
QCoreApplication.processEvents()
QCoreApplication.exit(exitCode)
self._shuttingDown = True
def isShuttingDown(self):
return self._shuttingDown
def quit(self, exitCode=0):
if self.mainWindow is not None:
self.mainWindow.close()
if self.settingsWindow is not None:
self.settingsWindow.close()
if self.serverThread != None and not sip.isdeleted(self.serverThread) and self.serverThread.isRunning():
self.serverThread.finished.disconnect(self.serverFinishedUnexpectedly)
get_server().stop_server()
getCoreLogger().info("Waiting maximal 30s for server to stop...")
示例2: MainWindow
# 需要导入模块: from PyQt4.QtGui import QSystemTrayIcon [as 别名]
# 或者: from PyQt4.QtGui.QSystemTrayIcon import contextMenu [as 别名]
class MainWindow(QMainWindow):
def __init__(self):
QMainWindow.__init__(self)
self.settings = QSettings(QSettings.IniFormat, QSettings.UserScope, VENDOR, APP)
self.setup = ConfigDialog(None, Qt.WindowSystemMenuHint | Qt.WindowTitleHint)
self.setup.setModal(True)
centerOnScreen(self.setup)
self.setup.wake.connect(self.wake)
self.setup.serversChanged.connect(self.updateMenu)
self.menuServers = []
self.trayIcon = QSystemTrayIcon(QIcon("res/power.png"), self)
self.trayIcon.activated.connect(self.activated)
menu = QMenu()
self.populateMenuFromSettings(menu)
menu.addSeparator()
self.setupAction = menu.addAction(QIcon('res/setup.png'), "Configure")
self.setupAction.triggered.connect(self.setup.show)
menu.addSeparator()
exitAction = menu.addAction("Exit")
exitAction.triggered.connect(self.close)
self.trayIcon.setContextMenu(menu)
self.trayIcon.setToolTip("Wake on LAN")
self.trayIcon.show()
servers = self.settings.beginReadArray("servers")
self.settings.endArray()
if not servers:
self.setup.show()
def populateMenuFromSettings(self, menu):
"""
:type menu: QMenu
"""
actions = menu.actions()
before = actions[0] if actions else None
title = QWidgetAction(menu)
label = QLabel("Hosts")
font = label.font()
px = font.pointSize()
font.setBold(True)
font.setPointSize(px * 1.5)
label.setFont(font)
label.setMargin(4)
label.setIndent(10)
# label.setStyleSheet("font-weight: bold; margin: 4px 2px; border-bottom: 2px solid black")
title.setDefaultWidget(label)
menu.insertAction(before, title)
self.menuServers.append(title)
servers = self.settings.beginReadArray("servers")
for d in range(servers):
self.settings.setArrayIndex(d)
server = Server.fromSettings(self.settings)
action = QAction(QIcon("res/server.png"), server.alias, menu)
menu.insertAction(before, action)
action.setData(server)
action.triggered.connect(self.wakeFromMenu)
self.menuServers.append(action)
self.settings.endArray()
def activated(self, reason):
if reason == QSystemTrayIcon.DoubleClick:
self.setup()
elif reason == QSystemTrayIcon.Trigger:
menu = QMenu()
self.populateMenuFromSettings(menu)
menu.exec_(QCursor.pos())
def updateMenu(self):
menu = self.trayIcon.contextMenu()
for action in self.menuServers:
action.setData(None)
menu.removeAction(action)
self.populateMenuFromSettings(menu)
def wakeFromMenu(self):
action = self.sender()
server = action.data().toPyObject()
self.wake(server)
def wake(self, server):
if QMessageBox.Yes == QMessageBox.question(self, "Wake on LAN", "Wake %s?" % server.alias, QMessageBox.Yes|QMessageBox.No):
magic = '\xFF' * 6
#.........这里部分代码省略.........