PyQt QShortcut
最后修改于 2023 年 8 月 24 日
在本文中,我们将展示如何使用 QShortcut。
源代码可在 PyQt-Examples 代码库中找到。访问 ,阅读 PyQt5 教程,或列出所有 PyQt 教程。
键盘快捷键是一个或多个按键的组合,用于在应用程序中执行某个操作。
QShortcut
QShortcut
用于将键盘快捷键连接到 PyQt 的信号和槽机制,这样当快捷键被执行时,对象就能收到通知。当用户键入某个快捷键的按键序列时,该快捷键的 activated
信号就会被发射。
快捷键可以被设置为包含描述一个键盘快捷键所需的所有按键操作,包括修饰键(如 Shift、Ctrl、Alt)的状态。
在某些控件上,在一个字符前使用 &
会自动为该字符创建一个助记符(在此上下文中,快捷键有时被称为助记符)。例如,&Quit
会创建快捷键 Alt + Q。在 Linux 上,该键下方会显示下划线。在 Windows 上,按下 Alt 键后,该键下方会显示下划线。在 Mac 上,助记符默认是禁用的。
在不支持自动助记符的控件上,我们使用 QShortcut
和 QKeySequence
来创建快捷键。
PyQt QShortcut 简单示例
下面的示例创建了两个快捷键。
#!/usr/bin/python from PyQt5.QtWidgets import QWidget, QShortcut, QApplication, QMessageBox from PyQt5.QtGui import QKeySequence import sys class Example(QWidget): def __init__(self): super().__init__() self.initUI() def initUI(self): self.msgSc = QShortcut(QKeySequence('Ctrl+M'), self) self.msgSc.activated.connect(lambda : QMessageBox.information(self, 'Message', 'Ctrl + M initiated')) self.quitSc = QShortcut(QKeySequence('Ctrl+Q'), self) self.quitSc.activated.connect(QApplication.instance().quit) self.setGeometry(300, 300, 300, 200) self.setWindowTitle('Shortcuts') self.show() def main(): app = QApplication(sys.argv) ex = Example() sys.exit(app.exec_()) if __name__ == '__main__': main()
Ctrl + M 快捷键会显示一个消息框,而 Ctrl + Q 会退出应用程序。
self.msgSc = QShortcut(QKeySequence('Ctrl+M'), self)
Ctrl + M 快捷键被创建。它应用于主窗口控件。
self.msgSc.activated.connect(lambda : QMessageBox.information(self, 'Message', 'Ctrl + M initiated'))
我们将一个显示消息框的 lambda 函数连接到快捷键的 activated
信号。
self.quitSc = QShortcut(QKeySequence('Ctrl+Q'), self) self.quitSc.activated.connect(QApplication.instance().quit)
Ctrl + Q 快捷键会终止应用程序。
QPushButton 上的 QShortcut
在下面的示例中,我们在一个按钮上创建了一个助记符快捷键。
#!/usr/bin/python from PyQt5.QtWidgets import (QWidget, QHBoxLayout, QApplication, QPushButton, QMessageBox, QSizePolicy) from PyQt5.QtGui import QKeySequence from PyQt5.QtCore import Qt import sys class Example(QWidget): def __init__(self): super().__init__() self.initUI() def initUI(self): hbox = QHBoxLayout() msgBtn = QPushButton('&Show message', self) msgBtn.clicked.connect(lambda : QMessageBox.information(self, 'Message', 'Information message')) hbox.addWidget(msgBtn) msgBtn.setSizePolicy(QSizePolicy.Fixed, QSizePolicy.Fixed) hbox.setAlignment(Qt.AlignLeft) self.setLayout(hbox) self.move(300, 300) self.setWindowTitle('Shortcuts') self.show() def main(): app = QApplication(sys.argv) ex = Example() sys.exit(app.exec_()) if __name__ == '__main__': main()
当按下 Alt + S 快捷键时,会显示一个消息框。
msgBtn = QPushButton('&Show message', self)
通过在按钮标签中使用 &
字符,创建了 Alt + S 快捷键。

使用 QAction 和 QMenu 的 QShortcut
我们可以使用 QAction
和 QMenu
创建快捷键。
#!/usr/bin/python import sys from PyQt5.QtWidgets import QMainWindow, QAction, qApp, QApplication from PyQt5.QtGui import QIcon class Example(QMainWindow): def __init__(self): super().__init__() self.initUI() def initUI(self): exitAct = QAction(QIcon('exit.png'), '&Exit', self) exitAct.setShortcut('Ctrl+Q') exitAct.setStatusTip('Exit application') exitAct.triggered.connect(qApp.quit) self.statusBar() menubar = self.menuBar() fileMenu = menubar.addMenu('&File') fileMenu.addAction(exitAct) self.setGeometry(300, 300, 300, 200) self.setWindowTitle('Simple menu') self.show() def main(): app = QApplication(sys.argv) ex = Example() sys.exit(app.exec_()) if __name__ == '__main__': main()
该示例有三个快捷键:Alt + F 用于打开“文件”菜单,然后 Alt + E 用于退出应用程序。这些快捷键必须按指定顺序激活。Ctrl + Q 无需操作菜单即可终止应用程序。
exitAct = QAction(QIcon('exit.png'), '&Exit', self)
通过在 QAction
的标签中使用 &
创建了 Alt + E 助记符。
exitAct.setShortcut('Ctrl+Q')
使用 setShortcut
函数创建了一个全局快捷键。
fileMenu = menubar.addMenu('&File')
我们使用 addMenu
函数创建了 Alt + F 助记符。

在本文中,我们介绍了 PyQt 的快捷键。
作者
列出所有 PyQt 教程。