关于pyqt5 manubar热键

2024-09-27 23:25:26 发布

您现在位置:Python中文网/ 问答频道 /正文

我试着自己写一个关于菜单栏的快捷方式示例,但是我遇到了一些问题。 我在菜单栏上有一个File(&F),在File(&F)中有一个保存文件(Ctrl+S)项,我想知道为什么我不能在按Alt + F后使用Ctrl + S的快捷方式

menuBar = self.menuBar()
fileMenu = menuBar.addMenu("&File")
self.fileMenu .addAction(self.SaveFileBt)
self.SaveFileBt.setShortcut("Ctrl+S")

Tags: 文件self示例altfile快捷方式ctrl菜单栏
1条回答
网友
1楼 · 发布于 2024-09-27 23:25:26

在下面的示例中,有三种方法可以调用save操作

  1. 按相应的菜单项,如图所示

  2. 使用键盘快捷键Ctrl+S

  3. 使用键盘快捷键Alt+F+S


import sys
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *


class MainWindow(QMainWindow):
    def __init__(self):
        super().__init__()

        menuBar    = self.menuBar()
        fileMenu   = menuBar.addMenu("&File")
        fileMenu.addAction("New")

        #          ->  & < -
        save = QAction("&Save", self)
        save.setShortcut("Ctrl+S")
        fileMenu.addAction(save)

        edit = fileMenu.addMenu("Edit")
        edit.addAction("copy")
        edit.addAction("paste")

        quit = QAction(QIcon("D:/_Qt/__Qt/img/exit.png"), "Quit",self)
        quit.setShortcut('Ctrl+Q')
        quit.triggered.connect(qApp.quit)
        fileMenu.addAction(quit)

        fileMenu.triggered[QAction].connect(self.processtrigger)     

    def processtrigger(self, q):
        print( "{} is triggered".format(q.text()))


if __name__ == '__main__':
    app = QApplication(sys.argv)
    ex = MainWindow()
    ex.setWindowTitle("Qmenu")
    ex.resize(350,300)
    ex.show()
    sys.exit(app.exec_())

enter image description here

相关问题 更多 >

    热门问题