2017-07-20 7 views
0

ドロップダウンメニューのショートカットを設定しようとしていました。コードは次のとおりですトレイドロップダウンメニューの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__(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が私のラップトップにインストールされました。

答えて

0

私はあなたとまったく同じ問題を抱えていた、と私は各アクションの次のパターンを尊重し、それを解決:

menu.addAction(exitAction) 
parent.addAction(exitAction) # line to be added 
+0

はあなたの助けをいただき、ありがとうございます。私はあなたのコードを試します。 – l0o0

関連する問題