6
簡單地說 - 我如何在PyQt5中做鍵盤快捷鍵(來運行一個函數)?我看到我以某種方式假設QAction
,但我不能將這兩個和兩個放在一起,所有示例似乎都不適用於PyQt5。由於PyQt5:鍵盤快捷鍵w/QAction
簡單地說 - 我如何在PyQt5中做鍵盤快捷鍵(來運行一個函數)?我看到我以某種方式假設QAction
,但我不能將這兩個和兩個放在一起,所有示例似乎都不適用於PyQt5。由於PyQt5:鍵盤快捷鍵w/QAction
使用QShortcut
和QKeySequence
類是這樣的:
import sys
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
class Window(QWidget):
def __init__(self, *args, **kwargs):
QWidget.__init__(self, *args, **kwargs)
self.label = QLabel("Try Ctrl+O", self)
self.shortcut = QShortcut(QKeySequence("Ctrl+O"), self)
self.shortcut.activated.connect(self.on_open)
self.layout = QHBoxLayout()
self.layout.addWidget(self.label)
self.setLayout(self.layout)
self.resize(150, 100)
self.show()
@pyqtSlot()
def on_open(self):
print("Opening!")
app = QApplication(sys.argv)
win = Window()
sys.exit(app.exec_())
另一個出色的答案 - 謝謝! – 2014-09-24 00:27:12