Skip to main content
 首页 » 编程设计

python之两个快捷键

2024年10月25日9mfrbuaa

我正在尝试在 Pyside/PyQt4 中设置没有修饰符的双键快捷方式。

我尝试了 QEvents 和 QKeySequences 的各种组合,但都无济于事。

我想做的是这样的:

def keyPressEvent(自身,事件): 如果 (event.key() == QtCore.Qt.Key_O) 和 (event.key() == QtCore.Qt.Key_P ): print('接受快捷方式')

或

fileMenu.addAction(QtGui.QAction("Open Project", self, shortcut=QtGui.QKeySequence("P" and "O"),triggered=self.openProject)) 

用户按下 O,然后按下 P,然后 Action 发生。

有没有人首先知道这是否可行,如果可行怎么办?

请您参考如下方法:

对我来说,"O, P" 作为 QKeySequence 的参数来完成这项工作。

例子:

from PySide import QtGui 
 
def beep(): 
    print('beep') 
 
app = QtGui.QApplication([]) 
 
toolbar = QtGui.QToolBar() 
toolbar.show() 
 
action = QtGui.QAction("Action", toolbar, shortcut=QtGui.QKeySequence("O, P"), triggered=beep) 
toolbar.addAction(action) 
 
app.exec_()