0
class MenuBarWidget(QMenuBar):
    sig_new_file = pyqtSignal()
    sig_open_file = pyqtSignal()
    sig_save_file = pyqtSignal()
    sig_page_setup = pyqtSignal()
    sig_print = pyqtSignal()
    sig_exit = pyqtSignal()
    sig_status_bar = pyqtSignal(bool)

def __init__(self):
    super().__init__()
    self.init_menu_file()

    # self.init_menu_edit()
    # self.init_menu_format()
    # self.init_menu_view()
    # self.init_menu_help()
    # self.set_all_text()

def init_menu_file(self):
    self.act_new_file = QAction('File', self)
    self.act_new_file.setShortcut(QKeySequence('Ctrl+N'))
    self.act_new_file.triggered.connect(lambda: self.sig_new_file.emit())

    self.act_open_file = QAction('Open', self)
    self.act_open_file.setShortcut(QKeySequence('Ctrl+O'))
    self.act_new_file.triggered.connect(lambda: self.sig_open_file.emit())

    self.act_save_file = QAction('Save', self)
    self.act_save_file.setShortcut(QKeySequence('Ctrl+S'))
    self.act_save_file.triggered.connect(lambda: self.sig_save_file.emit())

    self.act_print = QAction('Print', self)
    self.act_print.setShortcut(QKeySequence('Ctrl+P'))
    self.act_print.triggered.connect(lambda: self.sig_print.emit())

    self.act_quit = QAction('Quit', self)
    self.act_quit.setShortcut(QKeySequence('Ctrl+Q'))
    self.act_quit.triggered.connect(lambda: self.sig_exit.emit())

    self.menu_file = self.addMenu('&File')
    self.menu_file.addAction(self.act_new_file)
    self.menu_file.addAction(self.act_open_file)
    self.menu_file.addAction(self.act_save_file)
    self.menu_file.addSeparator()
    self.menu_file.addAction(self.act_print)
    self.menu_file.addSeparator()
    self.menu_file.addAction(self.act_quit)


if __name__ == '__main__':

class Form(QMainWindow):

    sig_new_file = pyqtSignal()

    def __init__(self):
        super().__init__()
        self.initUI()

    def initUI(self):
        self.resize(640, 480)
        self.setWindowTitle('MenuBar')
        self.menu = MenuBarWidget()
        self.setMenuBar(self.menu)

app = QApplication(sys.argv)
form = Form()
form.show()
exit(app.exec_())

我想用 PyQt 5.15 在 MacOs 10.15.6 上制作一个窗口的菜单栏

我将 QMenuBar 类用于自定义菜单栏,在“文件”菜单下创建了一些子菜单并将其添加到 QMainWindow 中,我可以看到在显示顶部创建了全局菜单栏,就像其他 MacO 一样。

我都试过了

QMenuBar()  , QMainWIndow.setMenuBar()
sub_menu = QAction('Someting else..',self)

fileMenu = QMainWindow.menuBar().addMenu('File')
fileMenu.addAction(sub_menu)

但是创建的全局菜单栏不可点击。我点击了它,但它没有在“文件”菜单下方显示子菜单。

但是这种使用下面的'setNativeMenuBar(False)'的方式可以像Windows应用程序一样工作,但我想使用本机MacOs菜单栏

QMainWindow.menuBar() and QMainWindow.setNativeMenuBar(False)

这是图像。

https://imgur.com/a/Yp6c9YW

QMainWindow 和 MacOS 通用菜单栏

4

2 回答 2

1
# This worked on PyQt5 on macOS Big Sur (ver 11.6)
#    it is the result on painful trial and error
#    and reading the Qt Docs

    def _createMenuBar(self):
        menuBar = self.menuBar()

        # File menu
        fileMenu = menuBar.addMenu("File")
        fileMenu.addAction("New")

        save = QAction("Save",self)
        save.setShortcut("Ctrl+S")
        fileMenu.addAction(save)

        quit = QAction("\0Quit",self)
        quit.setShortcut("Ctrl+Q")
        fileMenu.addAction(quit)

        # Edit menu
        editMenu = menuBar.addMenu("Edit")
        editMenu.addAction("Copy")
        editMenu.addAction("Paste")

        # Help menu
        helpMenu=menuBar.addMenu("Help")
        helpMenu.addAction(self.helpContentAction)
        helpMenu.addAction(self.aboutAction)
于 2021-11-05T04:15:48.527 回答
0

这可能有助于 PyQt5.QtWidgets import QMenuBar

Mainmenu=self.menuBar();# 创建一个菜单栏小部件

Filemenu=Mainmenu.addMenu('file');#this 添加文件到菜单栏

Editmenu=Mainmenu.addMenu('Edit');#this 添加编辑到菜单栏

于 2020-10-06T09:31:24.660 回答