Python 快捷方式不';t在托盘下拉菜单中的PyQt5中工作
我试图为下拉菜单设置快捷方式。代码如下Python 快捷方式不';t在托盘下拉菜单中的PyQt5中工作,python,pyqt5,Python,Pyqt5,我试图为下拉菜单设置快捷方式。代码如下 import sys from PyQt5 import QtWidgets, QtCore, QtGui from PyQt5.QtWidgets import QAction class SystemTrayIcon(QtWidgets.QSystemTrayIcon): def __init__(self, icon, parent=None): super(SystemTrayIcon, self).__init__(i
import sys
from PyQt5 import QtWidgets, QtCore, QtGui
from PyQt5.QtWidgets import QAction
class SystemTrayIcon(QtWidgets.QSystemTrayIcon):
def __init__(self, icon, parent=None):
super(SystemTrayIcon, self).__init__(icon, parent)
menu = QtWidgets.QMenu(parent)
# add actions and shortcuts for them.
exitAction = QAction('Exit', self)
exitAction.setShortcut('Ctrl+Q')
exitAction.triggered.connect(parent.close)
menu.addAction(exitAction)
restoreAction = QAction('Restore', self)
restoreAction.setShortcut("Ctrl+R")
restoreAction.triggered.connect(self.restore)
menu.addAction(restoreAction)
self.setContextMenu(menu)
def restore(self):
print 'restore'
class MainWindow(QtWidgets.QWidget):
def __init__(self):
super(MainWindow, self).__init__()
self.initUI()
def initUI(self):
self.setGeometry(300, 300, 250, 150)
self.setWindowTitle('Icon')
self.tray_icon = SystemTrayIcon(QtGui.QIcon('/opt/WizNote/wiznote.png'), self)
self.tray_icon.show()
self.show()
if __name__ == '__main__':
app = QtWidgets.QApplication([])
w = MainWindow()
sys.exit(app.exec_())
这条捷径行不通。我的代码有问题吗?
我的笔记本电脑中安装了Python2.7和PyQt5。我遇到了与您完全相同的问题,我通过遵守每个操作的以下模式来解决它:
menu.addAction(exitAction)
parent.addAction(exitAction) # line to be added