📜  menubar pyqt - Python (1)

📅  最后修改于: 2023-12-03 15:17:36.800000             🧑  作者: Mango

Menubar in PyQt

A menubar is a graphical control element which contains drop-down menus used in GUI programs. PyQt provides a module named QMenuBar to implement menubar in our Python applications.

Create a Menubar

To create a menubar, we need to follow the steps below:

  1. Create a QMainWindow object.
  2. Create a QMenuBar object using the QMainWindow object's menuBar() method.
  3. Create QMenu objects using the QMenuBar object's addMenu() method.
  4. Create QAction objects for each menu item using the QMenu object's addAction() method.

Here is an example code snippet for creating a simple menubar with a single menu item:

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

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

        # create menubar
        menubar = self.menuBar()

        # create menu
        file_menu = QMenu('File', self)

        # create menu item
        exit_action = QAction('Exit', self)
        exit_action.triggered.connect(self.close)
        file_menu.addAction(exit_action)

        # add menu to menubar
        menubar.addMenu(file_menu)

if __name__ == '__main__':
    app = QApplication(sys.argv)
    main_window = MainWindow()
    main_window.show()
    sys.exit(app.exec_())

In this example, we create a MainWindow object, and then create a menubar using its menuBar() method. Next, we create a single menu item named "File" using the QMenu class, and then create a single "Exit" action using the QAction class. Finally, we add the "Exit" action to the "File" menu using the addAction() method, and add the "File" menu to the menubar using the addMenu() method.

Customizing Menubar

We can customize the appearance of the menubar by setting properties such as font, color, and size. We can also add icons to menu items or even create custom widgets.

Here is an example code snippet for creating a more complex menubar:

import sys
from PyQt5.QtWidgets import QApplication, QMainWindow, QMenuBar, QMenu, QAction, QGroupBox, QCheckBox


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

        # create menubar
        menubar = self.menuBar()

        # create menu items
        file_menu = QMenu('File', self)
        edit_menu = QMenu('Edit', self)
        help_menu = QMenu('Help', self)

        # create menu item sub-actions
        new_act = QAction('New', self)
        open_act = QAction('Open', self)
        save_act = QAction('Save', self)
        exit_act = QAction('Exit', self)
        cut_act = QAction('Cut', self)
        copy_act = QAction('Copy', self)
        paste_act = QAction('Paste', self)
        help_act = QAction('Help Content', self)

        # add sub-actions to menu items
        file_menu.addAction(new_act)
        file_menu.addAction(open_act)
        file_menu.addAction(save_act)
        file_menu.addSeparator()
        file_menu.addAction(exit_act)
        edit_menu.addAction(cut_act)
        edit_menu.addAction(copy_act)
        edit_menu.addAction(paste_act)
        help_menu.addAction(help_act)

        # add menu items to menubar
        menubar.addMenu(file_menu)
        menubar.addMenu(edit_menu)
        menubar.addMenu(help_menu)

        # create custom widget
        self.group_box = QGroupBox('Settings', self)
        self.check_box1 = QCheckBox('Option 1', self)
        self.check_box2 = QCheckBox('Option 2', self)
        self.group_box.setLayoutVertical(self.check_box1, self.check_box2)

        # add custom widget to main window
        self.setCentralWidget(self.group_box)


if __name__ == '__main__':
    app = QApplication(sys.argv)
    main_window = MainWindow()
    main_window.show()
    sys.exit(app.exec_())

In this example, we create a more complex menubar with three menus named "File", "Edit", and "Help". Each of these menus contains several sub-actions that can be triggered by the user. We also create a custom widget named "Settings" that contains two check boxes named "Option 1" and "Option 2", and add it to the main window using the setCentralWidget() method.

Conclusion

In conclusion, PyQt provides a simple and easy-to-use way to add menubar to our Python applications. We can customize the appearance of the menubar by setting properties such as font, color, and size, and add icons or custom widgets to menu items. With this knowledge, we can create powerful and user-friendly graphical applications that incorporate a menubar for easy navigation.