1

我想在我的主窗口中有一个菜单栏,并且能够从其他类中设置菜单栏中的菜单。使用 setMenuWidget 命令将覆盖第一个菜单选项,如代码所示。在我设置菜单的类中,我想我可能只需要设置一个菜单而不是菜单栏,然后​​在主窗口中设置菜单栏。

这就是我想要的,可以通过在类中填充单个菜单栏来实现,尽管我试图避免这种方法。

在此处输入图像描述

而是只显示第二个菜单

在此处输入图像描述

import sys
from PyQt5.QtWidgets import QAction, QApplication, QMainWindow
from PyQt5 import QtCore, QtGui, QtWidgets

class ToolBar0(QMainWindow):
    def __init__(self, parent=None):
        QMainWindow.__init__(self)

        bar = self.menuBar() # don't think I need a menubar here
        file_menu = bar.addMenu('menu1')
        one = QAction('one', self)
        two = QAction('two', self)
        file_menu.addAction(one)
        file_menu.addAction(two)


class ToolBar1(QMainWindow):
    def __init__(self, parent=None):
        QMainWindow.__init__(self)

        bar = self.menuBar() # don't think I need a menubar here
        file_menu = bar.addMenu('menu2')
        one = QAction('one', self)
        two = QAction('two', self)
        file_menu.addAction(one)
        file_menu.addAction(two)


class MainWindow(QMainWindow):
    def __init__(self):
        QMainWindow.__init__(self, parent=None)

        #should a menubar be set up here?

        #For seting widgets in main window
        self.Tool_Bar0 = ToolBar0(self)
        self.setMenuWidget(self.Tool_Bar0)

        ###menu_bar0 is over written
        self.Tool_Bar1 = ToolBar1(self)
        #self.setMenuWidget(self.Tool_Bar1)


if __name__ == '__main__':
    app = QApplication(sys.argv)
    # creating main window
    mw = MainWindow()
    mw.show()
    sys.exit(app.exec_())
4

1 回答 1

1

您可以使用带有方法的基类来返回QMenu包含QAction项目的项目列表或QAction项目列表,然后QMainWindow以任何您想要的方式在工具栏中呈现它们,这是一个示例:

import sys
from PyQt5.QtWidgets import QAction, QApplication, QMainWindow, QMenu


class WindowWithToolbar:
    def __init__(self):
        super().__init__()

    def menu_items(self)->list:
        pass


class Window1(WindowWithToolbar, QMainWindow):
    def __init__(self):
        WindowWithToolbar.__init__(self)
        QMainWindow.__init__(self)

        # New menu with actions
        self.menu = QMenu('one')
        self.menu.addActions([QAction('two', self), QAction('three', self)])

    def menu_items(self):
        return [self.menu]


class Window2(WindowWithToolbar, QMainWindow):
    def __init__(self):
        WindowWithToolbar.__init__(self)
        QMainWindow.__init__(self)

    def menu_items(self):
        # Only actions
        return [QAction('three', self), QAction('four', self)]


class MainWindow(WindowWithToolbar, QMainWindow):
    def __init__(self):
        QMainWindow.__init__(self, parent=None)

        self.window1 = Window1()
        self.window2 = Window2()

        self.menu = QMenu('File')
        self.helloAction = QAction('Hello')
        self.menu.addAction(self.helloAction)

        self._build_menu()

    def menu_items(self)->list:
        return [self.menu]

    def _build_menu(self):
        self._add_menu_items(self)
        self._add_menu_items(self.window1)
        self._add_menu_items(self.window2)

    def _add_menu_items(self, windowWithToolbar: WindowWithToolbar):
        for menu_item in windowWithToolbar.menu_items():
            if isinstance(menu_item, QMenu):
                self.menuBar().addMenu(menu_item)
            elif isinstance(menu_item, QAction):
                self.menuBar().addAction(menu_item)


if __name__ == '__main__':
    app = QApplication(sys.argv)
    mw = MainWindow()
    mw.show()
    sys.exit(app.exec_())
于 2019-08-27T13:49:52.220 回答